C# &引用;“级联”;数据绑定

C# &引用;“级联”;数据绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我是wpf的初学者,我想帮助您处理这段XAML代码 <DataGrid ItemsSource="{Binding Elements[person]}" > <DataGrid.Columns> <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Element[phone].Value}"> <DataGridTextC

我是wpf的初学者,我想帮助您处理这段XAML代码

    <DataGrid ItemsSource="{Binding Elements[person]}" >
       <DataGrid.Columns>
          <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Element[phone].Value}">
             <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}" x:Name="headerPhoneCStyle">
                   <Style.Triggers>
                      <DataTrigger Binding="{Binding Element[phone].Attribute[changed].Value}" Value="yes">
                      <Setter Property="Background" Value="Red"/>
                  </DataTrigger>
               </Style.Triggers>
            </Style>
         </DataGridTextColumn.CellStyle>
      </DataGridTextColumn>
   </DataGrid.Columns>
</DataGrid>
就像这样:

Binding="{Binding Attribute[changed].Value}"

为此,您需要更改项目的
DataContext
。当前,您的
DataGrid.ItemsSource
属性是绑定到
元素[person]
对象的数据。。。这必须是某种类型的集合,因此每个项的
DataContext
都设置为该集合中的一个元素

很难说出数据类的确切结构,因为您没有向我们显示[],但是如果您的
绑定
有效,那么似乎每个项中都有一个名为
元素
的索引属性。现在,如果您想直接与使用index
phone
时返回的对象进行数据绑定,只需将其添加到
项资源绑定中即可:

<DataGrid ItemsSource="{Binding Elements[person].Element[phone]}">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Value}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}" x:Name="headerPhoneCStyle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Attribute[changed].Value}" 
                            Value="yes">
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

但是,如果
Elements[person].Element[phone]
不是一个集合,那么使用
DataGrid
来显示它没有多大意义。。。如果它是一个集合,那么它应该可以正常工作。让我知道你进展如何。此外,您还应该查看MSDN上的和页面,以获取有关
Binding.Path
语法的更多帮助

Binding=“{Binding Attribute[changed].Value}”
这不起作用吗?
<DataGrid ItemsSource="{Binding Elements[person].Element[phone]}">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Value}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}" x:Name="headerPhoneCStyle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Attribute[changed].Value}" 
                            Value="yes">
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>