我想将Label
的内容绑定到的SelectedItem
。wpf datagrid当前项目绑定
我认为'当前项目'绑定表达式可以工作,但事实并非如此。
我的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; } }
}
}
的标签显示DataTable
的第一个项目,当我选择在DataGrid
其他项目不会改变。看来DataView
目前的项目不会改变。 我应该怎么做才能将它绑定到DataGrid
的当前SelectedItem
?