简体   繁体   中英

Selecting a row in DataGrid by clicking Row

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

<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"/>

列设置为*时

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

Have you tried this ?

customerListBox.SelectionUnit = DataGridSelectionUnit.FullRow;

You can also use

customerListBox.SelectionMode = DataGridSelectionMode.Extended;

To allow multiple selection if you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM