[英]wpf datagrid current item binding
I want to bind a content of a Label
to the SelectedItem
of a DataGrid
. 我想将
Label
的内容绑定到DataGrid
的SelectedItem
。
I thought the 'current item' binding expression would work, but it does not. 我认为'当前项'绑定表达式可以工作,但事实并非如此。
My xaml code and code-behind c# is like below: 我的xaml代码和代码隐藏c#如下所示:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="512" Width="847">
<DockPanel LastChildFill="True">
<Label Content="{Binding Data/colA}" DockPanel.Dock="Top" Height="30"/>
<DataGrid ItemsSource="{Binding Data}"></DataGrid>
</DockPanel>
</Window>
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyData();
}
}
public class MyData
{
DataTable data;
public MyData()
{
data = new DataTable();
data.Columns.Add("colA");
data.Columns.Add("colB");
data.Rows.Add("aa", 1);
data.Rows.Add("bb", 2);
}
public DataTable Data { get { return data; } }
}
}
The label shows the first item of the DataTable
, and does not change when I select other items on the DataGrid
. 标签显示
DataTable
的第一项,当我在DataGrid
上选择其他项时不会更改。 It seems the current item of DataView
does not change. 似乎
DataView
的当前项不会改变。 What should I do to bind it to the current SelectedItem
of the DataGrid
? 我该怎么做才能将它绑定到
DataGrid
的当前SelectedItem
?
试试这个
<Label Content = "{Binding ElementName = DataGridName, Path = SelectedItem}"/>
The binding in your Label
binds to Data
independently of the DataGrid
's binding to Data
. 在结合
Label
绑定到Data
独立的DataGrid
的结合Data
。 Try: 尝试:
<Label Content="{Binding SelectedValue, ElementName=TheGrid}" />
<DataGrid x:Name="TheGrid" ItemsSource="{Binding Data}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.