为什么如果我将两个控件(例如2个TextBoxes)绑定到未实现INotifyPropertyChanged的模型的单个属性,两个文本框的内容保持同步?另一个TextBox如何被通知另一个更新源?将两个WPF控件绑定到一个模型属性是否创建共享绑定?
型号:
namespace BindingExample
{
public class PersonModel
{
public string Name { get; set; }
}
}
查看:
<Window x:Class="BindingExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingExample"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:PersonModel x:Key="person"></local:PersonModel>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="100"/>
</Style>
</Window.Resources>
<Grid DataContext="{StaticResource person}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
你能显示你的代码吗?我会说这是预期的行为,但是你说你还没有实现'INotifyPropertyChanged' - 在这种情况下,我不希望'TextBox'要更新。 – ChrisF 2012-02-16 11:57:42
@ChrisF最初期望绑定只读取源属性的值。 – Clemens 2012-02-16 12:03:19
@Jason Sich是源属性也许是DependencyProperty,虽然这在模型类中会很奇怪。 – Clemens 2012-02-16 12:04:57