gpt4 book ai didi

WPF ToggleButton 不正确的呈现行为

转载 作者:行者123 更新时间:2023-12-01 11:01:44 24 4
gpt4 key购买 nike

这是怎么回事,如何解决?

  1. 按下 MyToggleButton。现在看起来像是立即按下(选中)。
  2. 同时 MyToggleButton_Checked 处理程序禁用 MyToggleButton。
  3. 现在按下非切换按钮“启用”。我们看到了什么? MyToggleButton 看起来已启用。好的。但是等等,现在它看起来像没有被压过!为什么?!

XAML:

<StackPanel>

<ToggleButton x:Name="MyToggleButton" Content="MyToggleButton" Checked="MyToggleButton_Checked"/>

<TextBlock Text="{Binding IsChecked, ElementName=MyToggleButton}" Margin="0,4"/>

<Button Name="EnableButton" Content="Enable" Click="EnableButton_Click"/>
<Button Name="DisableButton" Content="Disable" Click="DisableButton_Click"/>

</StackPanel>

代码隐藏:

void MyToggleButton_Checked(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = false;
}


void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;
}

void DisableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = false;
}

更新:

目前唯一可能的解决方法是:

void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;

var controlTemplate = MyToggleButton.Template;

var buttonChrome = (Microsoft.Windows.Themes.ButtonChrome)controlTemplate.FindName("Chrome", MyToggleButton);

buttonChrome.RenderPressed = false;

buttonChrome.RenderPressed = true;
}

还有其他的吗?

UPD2:

另一种解决方法是:

void EnableButton_Click(object sender, RoutedEventArgs e)
{
MyToggleButton.IsEnabled = true;

var controlTemplate = MyToggleButton.Template;
MyToggleButton.Template = null;
MyToggleButton.Template = controlTemplate;
}

但是在模板替换的那一刻,控件在闪烁。

最佳答案

这就是我对问题的解决方案,这里是 video , 用法:

<StackPanel>
<ToggleButton x:Name="MyToggleButton" Content="MyToggleButton" Checked="MyToggleButton_Checked"
l:CorrectToggleButtonCheckedEnableBehavior.IsActive="True"/>
<Button Name="EnableButton" Content="Enable" Click="EnableButton_Click"/>
</StackPanel>

和附加的行为源代码:

public static class CorrectToggleButtonCheckedEnableBehavior
{
public static bool GetIsActive(ToggleButton toggleButton)
{
return (bool)toggleButton.GetValue(IsActiveProperty);
}

public static void SetIsActive(ToggleButton toggleButton, bool value)
{
toggleButton.SetValue(IsActiveProperty, value);
}

public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(CorrectToggleButtonCheckedEnableBehavior),
new UIPropertyMetadata(false, OnIsActiveChanged));

static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var toggleButton = (ToggleButton)d;

if (GetIsActive(toggleButton))
{
toggleButton.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ToggleButton_IsEnabledChanged);
}
else
{
toggleButton.IsEnabledChanged -= new DependencyPropertyChangedEventHandler(ToggleButton_IsEnabledChanged);
}
}

static void ToggleButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var toggleButton = (ToggleButton)sender;

if (toggleButton.IsEnabled && (toggleButton.IsChecked ?? false))
{
Invalidate(toggleButton);
}
}

static void Invalidate(ToggleButton toggleButton)
{
var controlTemplate = toggleButton.Template;
toggleButton.Template = null;
toggleButton.Template = controlTemplate;
}
}

关于WPF ToggleButton 不正确的呈现行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013838/

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