C#WPF怎么将一个控件与自定义结构体属性关联?
我控件一次改变的时候只能改变Room.x,Room.y,Room.z中的一个值撒。。但是我要控件值改变的时候触发Room的set函数 展开
2019-08-23
private double x;
public double X {
get { return x; }
set {
x = value;
set();//每次赋值时都调用 set()
}
}
private double y;
public double Y {
get { return y; }
set {
y = value;
set();//每次赋值时都调用 set()
}
}
private double z;
public double Z {
get { return z; }
set {
z = value;
set();//每次赋值时都调用 set()
}
}
在属性中,每次赋值(调用 set)时,都调用一次 set() 就好了
这个没问题。我没说清楚,不好意思哈,嗯。现在的问题是x. y. z是一个类。。然后room是另一个类里面的以xyz这个类为类型的自定义结构体变量。所以绑定的时候只触发了xyz的set。没有触发room的set
给你写个示例吧
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
xmlns:Properties="clr-namespace:WpfTest.Properties" x:Class="WpfTest.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,0,0,0">
<Label Content="X" HorizontalAlignment="Left" Margin="20,14,0,0" VerticalAlignment="Top"/>
<Label Content="Y" HorizontalAlignment="Left" Margin="20,43,0,0" VerticalAlignment="Top"/>
<Label Content="Z" HorizontalAlignment="Left" Margin="21,75,0,0" VerticalAlignment="Top"/>
<!--重点是UpdateSourceTrigger=PropertyChanged, Mode=TwoWay-->
<TextBox Name="textBox_X" Text="{Binding X, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Name="textBox_Y" Text="{Binding Y, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Name="textBox_Z" Text="{Binding Z, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsEnabled="False"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;//INotifyPropertyChanged
using System.Threading.Tasks;
namespace WpfTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private Room room = new Room();
public MainWindow()
{
InitializeComponent();
this.textBox_X.DataContext = room;
this.textBox_Y.DataContext = room;
this.textBox_Z.DataContext = room;
}
}
public class Room : DependencyObject
{
public int X
{
get { return (int)GetValue(XProperty); }
set {
SetValue(XProperty, value);
Console.WriteLine("set X={0}", value);
}
}
public int Y
{
get { return (int)GetValue(YProperty); }
set
{
SetValue(YProperty, value);
Console.WriteLine("set Y={0}", value);
}
}
public int Z
{
get { return (int)GetValue(ZProperty); }
set
{
SetValue(ZProperty, value);
Console.WriteLine("set Z={0}", value);
}
}
// UI控件的值变化时回调
private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Room room = d as Room;
switch (e.Property.Name)
{
case "X": room.X = (int)e.NewValue; break;
case "Y": room.Y = (int)e.NewValue; break;
case "Z": room.Z = (int)e.NewValue; break;
}
}
public static readonly DependencyProperty XProperty =
DependencyProperty.Register(
"X",
typeof(int),
typeof(Room),
new PropertyMetadata(0, OnPropertyChangedCallback, null)
);
public static readonly DependencyProperty YProperty =
DependencyProperty.Register(
"Y",
typeof(int),
typeof(Room),
new PropertyMetadata(0, OnPropertyChangedCallback, null)
);
public static readonly DependencyProperty ZProperty =
DependencyProperty.Register(
"Z",
typeof(int),
typeof(Room),
new PropertyMetadata(0, OnPropertyChangedCallback, null)
);
}
}
示例原文及运行效果参见我的博客 WPF—数据双向绑定
这个没问题。我没说清楚,不好意思哈,嗯。现在的问题是x. y. z是一个类。。然后room是另一个类里面的以xyz这个类为类型的自定义结构体变量。所以绑定的时候只触发了xyz的set。没有触发room的set