Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf XAML中的源与数据上下文_Wpf_Data Binding - Fatal编程技术网

Wpf XAML中的源与数据上下文

Wpf XAML中的源与数据上下文,wpf,data-binding,Wpf,Data Binding,以下哪种方法最好 <Window.Resources> <sys:Int16 x:Key="MyValue">123</sys:Int16> </Window.Resources> <StackPanel> <!-- method 1 --> <TextBlock Text="{Binding}" DataContext="{StaticResource MyValue}" />

以下哪种方法最好

<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>

123

我想说,如果我必须在两者之间做出选择,我会选择方法2。DataContext实际上更适合于将项目数据绑定到更复杂的底层对象,并简化了许多数据值的数据绑定


只是出于好奇,你为什么这样做?您的代码是否会在某个时刻更改MyValue的值?出于某种原因,您没有更好的方法来执行此操作吗?

DataContenxt DependencyProperty允许您轻松地跨DependencyObject的所有项目进行绑定

绑定的源依赖性属性允许您将特定绑定指向所需的源,而不考虑DataContext

当您为ListView执行更复杂的绑定时,这将非常有用。例如:

<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"......

就像许多“哪个更好”的问题一样。我会说“这取决于”上下文

它们都存在,因为它们都可以在不同的环境中发挥作用。仅考虑到上面所示,我将选择示例2

但是,设置DataContext时,其所有子项都将继承该DataContext。所以也许你用的是一个按钮。在你的按钮中,你想让它变得更加生动,用不同的颜色显示文本四次。正如您在下面看到的,我将选择示例1

示例1:(注意DataContext在按钮上,TextBlocks不像示例2中那样需要源代码)


例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最有意义


一个很好的经验法则。。。如果您发现自己将“Source”设置为同一事物的多个绑定,您可能应该只绑定一个公共父FrameworkElement的DataContext。

但我的问题是哪种方法是最好的。根据您需要如何处理其他绑定,哪种方法都可能是最好的方法。如果必须跨对象进行更多绑定,那么绑定到DataContext是最好的方法。如果仅仅是该属性的简单绑定,那么指定源代码是最好的;我从未实际使用过这样的数据绑定(使用单个值),但我想看看DataContext和Source之间是否有一种好的方法。谢谢你的贡献。
<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>