WPF新手,我有一个带有gridview的列表视图。
我想将ID列绑定到文本框的text属性,但我似乎无法弄清楚如何访问所选项目的各个网格列
代码如下所示
<ListView HorizontalAlignment="Left" Name="listView1" VerticalAlignment="Top"
SelectionChanged="onSelectedIndexChange" DataContext="{Binding Source={StaticResource xmlDoc}, Path=xmdModule}"
ItemsSource="{Binding XPath=//*[@ID]}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="0" DisplayMemberBinding="{Binding Path=Attributes[ID].Value}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Attributes[name].Value}" />
</GridView>
</ListView.View>
</ListView>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" Name="textBlock1" Text="{Binding Path=SelectedValue}"
VerticalAlignment="Top" DataContext="{Binding ElementName=listView1}" TextWrapping="Wrap"
TextAlignment="Left" />
答案 0 :(得分:1)
您可以尝试 将 SelectedValuePath
的{{1}}设置为ListView
属性:
ID
OR 尝试将<ListView .....
SelectedValuePath="Attributes[ID].Value"
.....>
绑定到TextBox
代替:
SelectedItem.Attributes[ID].Value
答案 1 :(得分:0)
试试这个 其中AttributeName是您要绑定的元素属性。
<GridViewColumn DisplayMemberBinding="{Binding XPath=@AttributeName}" Header="AttributeName" />
请参阅此链接以参考How to bind the xml element name to a ListView column in WPF XAML file
试过并且工作正常。