爲什麼如果我將兩個控件(例如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