gpt4 book ai didi

wpf - 如何让复选框在 WPF 数据网格中工作

转载 作者:行者123 更新时间:2023-12-05 05:25:31 25 4
gpt4 key购买 nike

我正在尝试向我的数据网格添加一个复选框列,以便用户可以轻松查看所选内容并轻松选择多个列,而无需知道如何使用 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>

最佳答案

问题是当您单击 CheckBox 时,当前 DataGridRowIsChecked 属性和 IsSelected 属性设置为true 同时。因此 IsChecked 绑定(bind)无法按预期方式工作。

解决此问题的一种方法(可能不是最好的方法)是防止通过单击选中 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}}}">

所以这意味着您的 CheckBox 仅在相应行被选中时才会被选中,但可以手动取消选中。

关于wpf - 如何让复选框在 WPF 数据网格中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com