DataFrid在WPF中的SelectedItem绑定

时间:2018-01-24 18:51:13

标签: c# wpf data-binding datagrid

我正在尝试获取SelectedItem的{​​{1}},但无论何时我添加" SelectedItem .."注释掉了,我运行应用程序时窗口没有显示。绑定有问题吗?

DataGrid

我正在使用ObservableCollection

        <StackPanel Grid.Column="2" Margin="40">
            <Label Content="Customer Table"/>
            <DataGrid Name="dgCustomer" 
                      AutoGenerateColumns="False" 
                      ItemsSource="{Binding Path=CustomerDataCollection}" 
                      IsReadOnly="True" 
                      TargetUpdated="dg_TargetUpdated"
                      SelectionMode="Single"
                      SelectionUnit="FullRow">
                <!--SelectedItem="{Binding Path=CustomerItemSelected, Mode=OneWayToSource}"-->
                <DataGrid.Columns>
                    <DataGridTextColumn Width="Auto" Header="ID" Binding="{Binding Id, NotifyOnTargetUpdated=True}"/>
                    <DataGridTextColumn Width="*" Header="Description" Binding="{Binding Description}"/>
                    <DataGridTextColumn Width="Auto" Header="OrderID" Binding="{Binding OrderID, NotifyOnTargetUpdated=True}"/>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>

1 个答案:

答案 0 :(得分:1)

您正在使用OneWayToSource绑定绑定到只读属性(CustomerItemSelected具有私有设置器)。这不起作用,所以让你的二传手公开:

public Customer CustomerItemSelected
{
    get => m_CustomerItemSelected;
    set => Set(ref m_CustomerItemSelected, value);
}