基于 DataTrigger 更改 Material WPF 复选框的边框颜色

change border color of a Material WPF checkbox based on DataTrigger

要更改 Wpf-Material 复选框边框颜色,需要覆盖它的资源 as follow:

<Style TargetType="Checkbox" BasedOn="{StaticResource MaterialDesignCheckBox}">
    <Style.Resources>
        <SolidColorBrush x:Key="MaterialDesignCheckBoxOff" Color="Red" />
    </Style.Resources>
</Style>

这有效,复选框边框变为红色。使用 Material 复选框时:<Setter Property="BorderBrush" Value="Red" /> 不起作用。

问题是我正在尝试动态应用此边框 属性。所以我为复选框创建了一个自定义组件,我在其中添加了一个名为 IsDirty 的布尔 DependencyProperty。 如果 IsDirty == true 和复选框 IsChecked,然后我更改背景(这很有效)。如果 IsDirty == true 并且复选框不是 IsChecked,那么在这种情况下我想更改边框颜色(因为背景是透明的)。 我尝试了如下操作:

<Style TargetType="{x:Type local:CustomCheckbox}" x:Key="dirtyCustomCheckbox" BasedOn="{StaticResource MaterialDesignCheckBox}">
    <Style.Resources>
        <SolidColorBrush x:Key="MaterialDesignCheckBoxOff" Color="Red" />
    </Style.Resources>
</Style>

<Style TargetType="{x:Type local:CustomCheckbox}" BasedOn="{StaticResource MaterialDesignCheckBox}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="True" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Background" Value="Red" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="False" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Style" Value="{StaticResource dirtyCustomCheckbox}" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

但我得到这个异常:ArgumentException:不允许样式对象影响它所应用的对象的样式 属性。

关于如何实现此目标的任何其他想法?

Any other idea on how I can achieve this?

您可以在自定义控件的构造函数中添加绑定到 Tag 属性 的资源:

public CustomCheckbox()
{
    var brush = new SolidColorBrush();
    BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, new Binding(nameof(Tag)) { Source = this, Mode = BindingMode.TwoWay });
    Resources.Add("MaterialDesignCheckBoxOff", brush);
}

然后你只需在 Style 中设置 Tag 属性:

<Style TargetType="{x:Type local:CustomCheckbox}"
       BasedOn="{StaticResource MaterialDesignCheckBox}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="True" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Background" Value="Red" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsDirty, RelativeSource={RelativeSource Self}}" Value="True" />
                <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="False" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Tag" Value="Red" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>