我有一个使用DependencyProperties(或INotifyPropertyChanged)的ViewModel,它具有非常简单的复合类型的属性,如System.Windows.Point.简单的复合类型不使用DependencyProperties或INotifyPropertyChanged,它意图保持这种方式(它不受我的控制).
我现在要做的是创建与Point的X和Y属性绑定的双向数据,但是当其中一个被更改时,我希望替换整个Point类,而不是只更新成员.
代码示例仅用于说明:
<Window ...>
<StackPanel>
<TextBox Text="{Binding TestPoint.X, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, StringFormat=\{0:F\}}"/>
<TextBox Text="{Binding TestPoint.Y, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, StringFormat=\{0:F\}}"/>
<!-- make following label update based on textbox changes above -->
<Label Content="{Binding TestPoint, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
public partial class MainWindow : Window
{
public Point TestPoint
{
get { return (Point)GetValue(TestPointProperty); }
set { SetValue(TestPointProperty, value); }
}
public static readonly DependencyProperty TestPointProperty = DependencyProperty.Register("TestPoint", typeof(Point), typeof(MainWindow), new PropertyMetadata(new Point(1.0, 1.0))); …Run Code Online (Sandbox Code Playgroud) c# data-binding wpf dependency-properties inotifypropertychanged