0

我看到 DataGrid 数据绑定语法如下:

ItemsSource="{Binding Path=ListDataColumns, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"

更完整的代码是:

    <DataGrid KeyboardNavigation.ControlTabNavigation="Local" KeyboardNavigation.IsTabStop="False"  DataContext="{Binding}"
              ItemsSource="{Binding Path=ListDataColumns, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
              AutoGenerateColumns="False" x:Name="DataGridColumnConfig" VerticalAlignment="Top" AllowDrop="True"
              IsSynchronizedWithCurrentItem="True" CanUserSortColumns="False" CanUserDeleteRows="False"
              CanUserAddRows="False"  GridLinesVisibility="All" SelectedItem="{Binding Path=SelectedItem}" SelectedIndex="{Binding Path=SelectedItemIndex}">

我认为代码绑定到一个名为“ListDataColumns”的属性。

如何从 ItemSource 确定绑定到哪个对象?

4

1 回答 1

1

要完全回答您的问题,我们需要您提供更多信息。但是,根据您提供的内容(并使用简化的 XAML 示例),我们可以告诉您以下内容:

<DataGrid DataContext="{Binding}" ItemsSource="{Binding Path=ListDataColumns, 
    Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, 
    UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=SelectedItem}" 
    SelectedIndex="{Binding Path=SelectedItemIndex}" ... />
  1. DataContext设置为与当前{Binding}相同,{Binding Path=.}并且意味着我们正在绑定到当前Binding.Source......换句话说,此控件的父级之一应该将对象的实例设置为它DataContext,并且此控件将共享同一个对象并可以访问相同的属性。
  2. ItemsSource属性设置为{Binding Path=ListDataColumns ...}which is 相同{Binding ListDataColumns ...},意味着它将查看任何对象设置为DataContext(父控件的)名为 的属性ListDataColumns
  3. SelectedItem属性设置为{Binding Path=SelectedItem}which is 相同{Binding SelectedItem},意味着它将查看任何对象设置为DataContext(父控件的)名为 的属性SelectedItem
  4. SelectedIndex属性设置为{Binding Path=SelectedItemIndex}which is 相同{Binding SelectedItemIndex},意味着它将查看任何对象设置为DataContext(父控件的)名为 的属性SelectedItem

这就是可以从您的 XAML 示例中获取的所有内容(忽略其他Binding属性)。但是,对于学习 XAML 和 WPF 的用户,以下是 MSDN 上一些非常有用的链接,可帮助您了解属性路径语法:

Binding.Path 属性

PropertyPath XAML 语法

属性路径语法

于 2013-10-29T09:23:04.823 回答