[英]wpf datagrid current item binding
我想將Label
的內容綁定到DataGrid
的SelectedItem
。
我認為'當前項'綁定表達式可以工作,但事實並非如此。
我的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
?
試試這個
<Label Content = "{Binding ElementName = DataGridName, Path = SelectedItem}"/>
在結合Label
綁定到Data
獨立的DataGrid
的結合Data
。 嘗試:
<Label Content="{Binding SelectedValue, ElementName=TheGrid}" />
<DataGrid x:Name="TheGrid" ItemsSource="{Binding Data}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.