gpt4 book ai didi

wpf - ToolKit DataGrid 在 LostFocus 上取消全选

转载 作者:行者123 更新时间:2023-12-03 22:49:56 26 4
gpt4 key购买 nike

我正在尝试创建一种样式,使我的所有 DataGrids 在失去焦点时都选择第 -1 行。我正在做:

<Style TargetType="{x:Type DataGrid}">
<Style.Triggers>
<EventTrigger RoutedEvent="DataGrid.LostFocus">
<BeginStoryboard>
<Storyboard>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="(DataGrid.SelectedIndex)">
<DiscreteInt32KeyFrame KeyTime="00:00:00" Value="-1" />
</Int32AnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>

它仅在第一次失去焦点时起作用,但在第二次由于类型转换异常而导致程序崩溃。是否有可能没有背后的代码?

最佳答案

根据我的研究,依附行为是我唯一可以接受的解决方案。希望这会帮助更多人:

public class DataGridBehavior
{
public static readonly DependencyProperty IsDeselectOnLostFocusProperty =
DependencyProperty.RegisterAttached("IsDeselectOnLostFocus", typeof(bool), typeof(DataGridBehavior), new UIPropertyMetadata(false, PropertyChangedCallback));

private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var dg = dependencyObject as DataGrid;
if (dg == null)
return;

if (e.NewValue is bool == false)
return;

if ((bool)e.NewValue)
dg.LostFocus += dg_LostFocus;
}

static void dg_LostFocus(object sender, RoutedEventArgs e)
{
(sender as DataGrid).SelectedIndex = -1;
}

public static bool GetIsDeselectOnLostFocus(DataGrid dg)
{
return(bool)dg.GetValue(IsDeselectOnLostFocusProperty);
}

public static void SetIsDeselectOnLostFocus(DataGrid dg, bool value)
{
dg.SetValue(IsDeselectOnLostFocusProperty, value);
}
}

然后:
<Style TargetType="{x:Type DataGrid}">
<Setter Property="helpers:DataGridBehavior.IsDeselectOnLostFocus" Value="True"/>
</Style>

关于wpf - ToolKit DataGrid 在 LostFocus 上取消全选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313211/

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