Selecting a row in DataGrid by clicking Row

时间:2017-06-15 10:34:27

标签: c# wpf xaml datagrid

I have a DataGrid that i am trying to change the selected item by selecting the row and not the cell. As you can see from the image below when i click outside the cell the item doesn't update.

GIF  of user trying to select row on blank cell

<DataGrid x:Name="customerListBox" SelectedItem="{Binding SelectedCustomer, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single" IsReadOnly="True"  ItemsSource="{Binding Customers}"  Margin="10,57,10,10"  AlternationCount="2"  BorderThickness="1" SnapsToDevicePixels="True"  AutoGenerateColumns="False" BorderBrush="Black" Foreground="Black">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Id}" Header="Id"/>
        <DataGridTextColumn Binding="{Binding Name}"  Header="Name"/>
        <DataGridTextColumn Binding="{Binding Phone}"  Header="Phone"/>
        <DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
    </DataGrid.Columns>
</DataGrid>

What i can do to get it working is setting the last column width to * However this makes the header central and looks messy when displayed on a wide screen monitor.

<DataGridTextColumn Binding="{Binding Email}" Width="*" Header="Email"/>

Column when set to *

2 个答案:

答案 0 :(得分:4)

我有解决此问题的解决方法,只需在最后一列之后添加虚拟空列宽度=“*”

答案 1 :(得分:1)

你试过这个吗?

customerListBox.SelectionUnit = DataGridSelectionUnit.FullRow;

您也可以使用

customerListBox.SelectionMode = DataGridSelectionMode.Extended;

如果需要,允许多项选择