1

要更改 Wpf-Material 复选框边框颜色,需要覆盖它的资源,如下所示:

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

这有效,复选框边框变为红色。使用材料复选框:<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:Style 对象不允许影响它所应用的对象的 Style 属性。

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

4

1 回答 1

1

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

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);
}

然后,您只需Tag在以下位置设置属性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="Tag" Value="Red" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>
于 2020-04-07T13:42:53.630 回答