开发者

How can you set the clickable area of a WPF datagrid cell?

开发者 https://www.devze.com 2023-03-12 14:06 出处:网络
In a datag开发者_StackOverflowrid where one column is editable and the other columns are read only.

In a datag开发者_StackOverflowrid where one column is editable and the other columns are read only.

The non editable column is bound to a nullable decimal field in a collection and is initially NULL, so no value is present in the column.

When trying to click this cell to get the datagrid into edit mode, the click target is very small and hard to click.

How can you set the click target size for such a cell?

How can you set the clickable area of a WPF datagrid cell?


In the datagrid, use Selection Unit="FullRow". Define a default DataGridCell Style and base all other DataGridCell styles on this. Then add a row style to the DataGrid. This gives you a way to select the whole row, and have another color for the Focused cell. And the entire cell background is in the color specified in the style.

   <Style TargetType="DataGridCell" x:Key="DgcDefault">
   <Setter Property="BorderBrush"  Value="Transparent"/>
   <Setter Property="Background" Value="Transparent"/>
   <Setter Property="Foreground" Value="Black"/>
   <Style.Triggers>
      <Trigger Property="IsFocused" Value="True">
        <Setter Property="Background" Value="#FF83B2DD"/>
       </Trigger>
   </Style.Triggers>
</Style>

<DataGrid.RowStyle>
   <Style TargetType="DataGridRow">
      <Setter Property="Background" Value="White"/>
      <Style.Triggers>
         <Trigger Property="AlternationIndex" Value="1">
            <Setter Property="Background" Value="AliceBlue"/>
         </Trigger>
         <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="LightBlue"/>
         </Trigger>
      </Style.Triggers>
   </Style>
</DataGrid.RowStyle>


If you are defining your own columns in the DataGrid, you could set the MinWidth property on the column so that there's always space to click on, even if the value is empty or very short. For example:

<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn
            MinWidth="100" 
            Header="Phone"
            Binding="{Binding Path=PhoneNumber}" />
    </DataGrid.Columns>
</DataGrid>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号