在WPF中使用数据绑定时,当通过INotifyPropertyChanged接口通知目标依赖项对象源已更改时,目标依赖项对象将更新。
例如:
<TextBlock Text="{Binding Path=SomeField}"/>每当从源调用PropertyChanged(this, new PropertyChangedEventArgs("SomeField"))时,文本字段都会更改以正确反映SomeField的值。
如果我使用如下所示的复杂路径:
<TextBlock Text="{Binding Path=SomeObjField.AnotherField}"/>文本字段是否会针对源上的PropertyChanged(this, new PropertyChangedEventArgs("SomeObjField"))进行更新?
那么中间对象(包含在SomeObjField中的对象)上的PropertyChanged(this, new PropertyChangedEventArgs("AnotherField"))呢?
源对象和字段不是依赖对象或属性!假设属性/类的实现如下所示:
public class Data : INotifyPropertyChanged
{
// INotifyPropertyChanged implementation...
public string SomeField
{
get { return val; }
set
{
val = value;
// fire PropertyChanged()
}
}
public SubData SomeObjField
{
get { return val; }
set
{
val = value;
// fire PropertyChanged()
}
}
}
public class SubData : INotifyPropertyChanged
{
// INotifyPropertyChanged implementation...
public string AnotherField
{
get { return val; }
set
{
val = value;
// fire PropertyChanged()
}
}
}发布于 2009-04-02 14:40:42
经过进一步研究,似乎当复杂路径的任何部分发送更改通知时,绑定都会更新。因此,如果源对象或中间对象被更改,则绑定将被更新。
我构建了一个类似Jared的测试项目:
<StackPanel Name="m_panel">
<TextBox IsReadOnly="True" Text="{Binding Path=SomeObjField.AnotherField }" />
<TextBox x:Name="field1"/>
<Button Click="Button1_Click">Edit Root Object</Button>
<TextBox x:Name="field2"/>
<Button Click="Button2_Click">Edit Sub Object</Button>
</StackPanel>和后面的代码:
public Window1()
{
InitializeComponent();
m_panel.DataContext = new Data();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Data d = m_panel.DataContext as Data;
d.SomeObjField = new SubData(field1.Text);
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
Data d = m_panel.DataContext as Data;
d.SomeObjField.AnotherField = field2.Text;
}我正在使用我在问题中提供的基本数据实现。
发布于 2009-04-02 13:57:18
我不是100%确定你在PropertyChanged部分问的是什么。但是,如果涉及的属性都是DependencyProperty支持的属性,那么这应该会按预期工作。我画出了下面的例子
Window1.xaml
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Name="m_panel">
<TextBlock Text="{Binding Path=SomeField}" />
<TextBlock Text="{Binding Path=SomeField.AnotherField }" />
<Button Click="Button_Click">Update Root Object</Button>
<Button Click="Button_Click_1">Update Another Field</Button>
</StackPanel>
</Window>Window1.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
m_panel.DataContext = new Class1();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((Class1)m_panel.DataContext).SomeField = new Class2();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
((Class1)m_panel.DataContext).SomeField.AnotherField = "Updated field";
}
}以及这些类
public class Class1 : DependencyObject
{
public static DependencyProperty SomeFieldProperty = DependencyProperty.Register(
"SomeField",
typeof(Class2),
typeof(Class1));
public Class2 SomeField
{
get { return (Class2)GetValue(SomeFieldProperty); }
set { SetValue(SomeFieldProperty, value); }
}
public Class1()
{
SomeField = new Class2();
}
}
public class Class2 : DependencyObject
{
public static DependencyProperty AnotherFieldProperty = DependencyProperty.Register(
"AnotherField",
typeof(string),
typeof(Class2));
public string AnotherField
{
get { return (string)GetValue(AnotherFieldProperty); }
set { SetValue(AnotherFieldProperty, value); }
}
public Class2()
{
AnotherField = "Default Value";
}
}https://stackoverflow.com/questions/709683
复制相似问题