2011-08-04 24 views
2

以下哪種方法最好?XAML中的源代碼與DataContext的對比

<Window.Resources> 
    <sys:Int16 x:Key="MyValue">123</sys:Int16> 
</Window.Resources> 

<StackPanel> 

    <!-- method 1 --> 
    <TextBlock Text="{Binding}" DataContext="{StaticResource MyValue}" /> 

    <!-- method 2 --> 
    <TextBlock Text="{Binding, Source={StaticResource MyValue}}" /> 

</StackPanel> 

回答

3

與許多「哪個更好」的問題一樣。我會說「這取決於」上下文。

它們都存在,因爲它們都可以在不同的環境中起作用。只給出上面顯示的內容,我會選擇示例2.

但是,當您設置DataContext時,其所有子項都將繼承該DataContext。所以也許你正在使用Button。在你的按鈕中,你想讓它變得有點爵士樂,並用不同的顏色顯示四次。正如你可以看到下面,我會再選擇例1

例1:(注意DataContext的是按鈕,和他們一樣例2中做的TextBlocks不需要源)

<Button DataContext="{StaticResource MyValue}" Height="Auto" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding}" Foreground="Red" /> 
     <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding}" Foreground="Blue" /> 
     <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding}" Foreground="Yellow"/> 
     <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding}" Foreground="Green" /> 
    </Grid> 
</Button> 

例2:

<Button Height="Auto" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding, Source={StaticResource MyValue}}" Foreground="Red" /> 
     <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding, Source={StaticResource MyValue}}" Foreground="Blue" /> 
     <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding, Source={StaticResource MyValue}}" Foreground="Yellow"/> 
     <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding, Source={StaticResource MyValue}}" Foreground="Green" /> 
    </Grid> 
</Button> 

當你綁定到一個簡單的對象,只有具有像一個Int16的一個表示你的情況,你可能只打算綁定,一旦顯示該值,從而選擇2最有意義。

一個很好的經驗法則......如果您發現自己將多個綁定設置爲相同的「源」,您應該只需綁定一個常見父FrameworkElement的DataContext。

1

我會說,如果我不得不在兩者之間進行選擇,我會用方法2.去DataContext的是真的比較數據綁定的項目更復雜的底層對象,並簡化多個數據值的數據綁定。

出於好奇,你爲什麼這樣做?你的代碼在某個時候是否改變了MyValue的值?有沒有更好的辦法讓你出於某種原因去做?

+0

我只是試圖想出一個簡單的例子來問這個問題;我從來沒有真正將數據綁定到任何類似的東西上(只有一個值),但我正在深入探討DataContext和Source之間是否有一個好的方法。感謝您的貢獻。 –

1

DataContenxt DependencyProperty允許您輕鬆綁定DependencyObject的所有proeprties。

綁定的源DependenceyProperty允許您將指定的源綁定到所需的源,而不考慮DataContext。

當你爲ListViews做更復雜的綁定時,這會變得非常有用。例如:

<Window.Resources> 
    <local:MyConverter x:Key="MyConverter" /> 
</Window.Resources> 
<Grid> 
    <ComboBox ItemsSource="{Binding Source={StaticResource MyConverter}, Path=DisplayValues}" DataContenxt={Binding ElementName=lvwItems Path=SelectedItem} SelectedItem="{Binding Converter={StaticResource MyConverter}"/> 
<ListView Name="lvwItems"...... 

上面的例子只是展示了我設置的ItemsSource的財產被稱爲DisplayValues的「MyConverter」,DataContext的是什麼,我對那個組合框,雖然工作,這是處理的SelectedItem名爲「lvwItems」的ListView屬性。

希望這會有所幫助。

+1

但我的問題是關於哪個是最好的方法。 –

+1

要麼是最好的方法,這取決於你如何處理其他綁定。 如果您必須在整個對象上執行更多綁定,則綁定到DataContext是最好的方法。 如果僅僅是該屬性的簡單綁定,則指定源是最好的。 –