2012-02-16 24 views
2

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

你能顯示你的代碼嗎?我會說這是預期的行爲,但是你說你還沒有實現'INotifyPropertyChanged' - 在這種情況下,我不希望'TextBox'要更新。 – ChrisF 2012-02-16 11:57:42

+0

@ChrisF最初期望綁定只讀取源屬性的值。 – Clemens 2012-02-16 12:03:19

+0

@Jason Sich是源屬性也許是DependencyProperty,雖然這在模型類中會很奇怪。 – Clemens 2012-02-16 12:04:57

回答

1

在您的示例中的兩個文本框都使用相同的datacontext,實際上共享.NET對象PersonModel的同一個實例,因爲默認設置的資源字典中的靜態資源是WPF 共享,即當引用到靜態資源時,您將始終獲得相同的實例。更多關於在X:MSDN上的共享屬性:

MSDN article about x:Shared attribute

的替代品,這是設置X:共享爲false,以每次得到一個新的實例。另外,我不得不在你的示例中刪除綁定到datacontext,以便個別地編輯文本框中的值。

下面的XAML顯示這一點:

<Window x:Class="WpfScrollToEndBehavior.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfScrollToEndBehavior" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <local:PersonModel x:Key="person" x:Shared="false" Name="John Doe"> </local:PersonModel> 
    <Style TargetType="TextBox"> 
     <Setter Property="VerticalAlignment" Value="Center" /> 
     <Setter Property="Width" Value="100"/> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Source={StaticResource person}}" /> 
    <TextBox Grid.Row="1" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Source={StaticResource person}}" /> 
</Grid> 

原因X:共享在資源字典中設置爲true的資源是出於效率的考慮。通過在XAML中的資源字典中的資源(例如對象)上設置x:Shared,每次資源被引用和訪問時,都會給我們新的實例。我必須從網格中刪除數據上下文,並將文本框的綁定源設置爲靜態資源人員。

+0

感謝您的回答。我正在學習WPF,從我所知道的這種行爲開始看起來不對。 – 2012-04-22 11:16:50

+1

答案「因爲文本框共享相同的datacontext對象」並不能解釋爲什麼兩個文本框同步。 這將是如果PersonModel實現'INotifyPropertyChanged'的答案。但事實並非如此。 該過程如下所示: - 說textbox1.Text由用戶更改。 - 它綁定到PersonModel.Name,因此它更新屬性。 但是,textbox2.Text怎麼知道這個Name屬性的新值呢? PersonModel對象不通知其屬性的更改。 – Lu55 2012-11-22 21:09:03

相關問題