我正在尝试向我的数据网格添加一个复选框列,以便用户可以轻松查看所选内容并轻松选择多个列,而无需知道如何使用CTRL按钮。有人会介意协助吗?
复选框将取消选中是否已选中,但我点击它时无法检查它。
<Window.Resources>
<DataTemplate x:Key="isSelectedCheckBoxColumn">
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="1.5" ScaleY="1.5" />
</CheckBox.LayoutTransform>
</CheckBox>
</DataTemplate>
<Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Background" Value="GhostWhite"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="ContextMenu" Value="{x:Null}"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#C1FFC1"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#F9F99F"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="Validation.HasError" Value="True" >
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="Red" ShadowDepth="0" BlurRadius="20" />
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="12" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="CenterCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid>
<ContentPresenter HorizontalAlignment="center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RightCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid>
<ContentPresenter HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DataGrid Grid.Row="1" AutoGenerateColumns="False" Name="grid_achCredit" RowStyle="{StaticResource RowStyleWithAlternation}" AlternationCount="2" FontSize="15" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTemplateColumn Header="" CellTemplate="{StaticResource isSelectedCheckBoxColumn}" IsReadOnly="False"/>
<DataGridTextColumn Header="ID" Width="Auto" IsReadOnly="False" Binding="{Binding ID}" />
<DataGridTextColumn Header="TransDate" Width="Auto" IsReadOnly="False" Binding="{Binding transactionDate, StringFormat=\{0:MM-dd-yyyy\}}" />
<DataGridTextColumn Header="Payor" Width="Auto" IsReadOnly="False" Binding="{Binding payer}" />
<DataGridTextColumn Header="Description" Width="*" IsReadOnly="False" Binding="{Binding description}" />
<DataGridTextColumn Header="Amount" Width="Auto" IsReadOnly="False" Binding="{Binding amount, StringFormat=C}" />
<DataGridTextColumn Header="Balance" Width="Auto" IsReadOnly="False" Binding="{Binding amount, StringFormat=C}" />
<DataGridTextColumn Header="Selected By" Width="*" IsReadOnly="False" Binding="{Binding lockedUser}"/>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
问题是当您同时点击设置为CheckBox
的当前IsChecked
的{{1}} IsSelected
属性和DataGridRow
属性时即可。因此,true
绑定不会按预期方式工作。
解决此问题的方法(可能不是最好的方法)是阻止通过单击检查IsChecked
(仅允许取消选中)。这可以通过仅在选中时启用CheckBox
来实现:
CheckBox
这意味着只有在选择了相应的行时才会检查 <CheckBox IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}">
,但可以手动取消选中。
答案 1 :(得分:1)
我最终决定使用行标题来设置复选框。这是我添加到我的datagrid
的代码 <DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}}}" Margin="0,-3,0,0">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</CheckBox.LayoutTransform>
</CheckBox>
</Grid>
</DataTemplate>
</DataGrid.RowHeaderTemplate>