3

这里发生了什么,以及如何解决?

  1. 按我的切换按钮。现在它看起来像立即按下(选中)。
  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;
}

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

4

1 回答 1

6

这是我解决问题的方法,这里是视频,用法:

<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;
    }
}
于 2012-04-07T08:59:01.803 回答