如何定义一种样式来查看其属性的值

时间:2018-08-07 16:33:57

标签: c# wpf

因此,我为切换按钮定义了样式,该样式可在按下ToggleButton时设置背景颜色。

<Style x:Key="ToggleStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Border CornerRadius="10" Margin="5,5,5,5" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"
                        Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Background" Value="DarkGray"/>
        </Trigger>
    </Style.Triggers>
</Style>

这可行,但是我真正要做的是根据我可以在XAML中为切换按钮定义的属性,使此样式设置不同的颜色。

这甚至可能吗?如果是这样,怎么办?

- 因此,埃里克(Eric)在下面的回答就起作用了,但是现在我发现自己想在每个实例的基础上同时修改背景和前景。

我尝试了以下操作:

    public static class ToggleButtonAttach
{
    private static readonly DependencyProperty CheckedBackgroundProperty = DependencyProperty.RegisterAttached("CheckedBackground", typeof(Brush), typeof(ToggleButtonAttach));
    public static void SetCheckedBackground(ToggleButton target, Brush value) => target.SetValue(CheckedBackgroundProperty, value);
    public static Brush GetCheckedBackground(ToggleButton target) => (Brush)target.GetValue(CheckedBackgroundProperty);

    private static readonly DependencyProperty CheckedForegroundProperty = DependencyProperty.RegisterAttached("CheckedForeground", typeof(Brush), typeof(ToggleButtonAttach));
    public static void SetCheckedForeground(ToggleButton target, Brush value) => target.SetValue(CheckedForegroundProperty, value);
    public static Brush GetCheckedForeground(ToggleButton target) => (Brush)target.GetValue(CheckedForegroundProperty);

}


       <Style x:Key="ToggleStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Border x:Name="border" Focusable="False" CornerRadius="10" Margin="5,5,5,5" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"
                            Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter x:Name="toggleButtonContentPresenter" TextElement.Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="TextElement.Foreground" TargetName="toggleButtonContentPresenter" Value="{Binding Path=(piapp:ToggleButtonAttach.CheckedForeground), RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Setter TargetName="border" Property="Background" Value="{Binding Path=(piapp:ToggleButtonAttach.CheckedBackground), RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
         </Style>

但是我遇到以下错误:“成员“ CheckedForeground”无法识别或无法访问。

显然有什么问题,但我没有看到。

3 个答案:

答案 0 :(得分:0)

听起来好像您想要一个带有附加dependency properties的自定义控件。从文章:

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
    typeof(MyCode)
    );

public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

您的控件模板然后可以将各种元素及其颜色属性绑定到您各自的依赖项属性。

答案 1 :(得分:0)

如果创建附加属性,则可以将其动态设置为给定画笔,并将该画笔用作ControlTemplate中的背景:

C#

public static class ToggleButtonInfo
{
    private static readonly DependencyProperty CheckedBackgroundProperty = DependencyProperty.RegisterAttached("CheckedBackground", typeof(Brush), typeof(ToggleButtonInfo));

    public static void SetCheckedBackground(ToggleButton target, Brush value) => target.SetValue(CheckedBackgroundProperty, value);
    public static Brush GetCheckedBackground(ToggleButton target) => (Brush)target.GetValue(CheckedBackgroundProperty);
}

XAML

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:wpfApp1="clr-namespace:WpfApp1"
    x:Class="WpfApp1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="ToggleStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border x:Name="border"  CornerRadius="10" Margin="5,5,5,5" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"
                                Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="border" Property="Background" Value="{Binding Path=(wpfApp1:ToggleButtonInfo.CheckedBackground), RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <ToggleButton Content="Hello" wpfApp1:ToggleButtonInfo.CheckedBackground="{x:Static Brushes.Fuchsia}" Style="{StaticResource ToggleStyle}"></ToggleButton>
</Window>

答案 2 :(得分:0)

您提到“这可行,但是我真正想要的是...”我不确定在不覆盖控制模板的情况下该如何运作。 以下代码块将初始切换按钮的颜色设置为Green,并且每当切换按钮时,都会设置背景颜色,即您喜欢设置的属性。这会给你一个想法。

<Style x:Key="ToggleStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border Name="border" CornerRadius="10" Margin="5,5,5,5" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"
                        Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger  Property="IsChecked" Value="True">
                                <Setter  TargetName="border" Property="Background" Value="{DynamicResource Background}"/>
                            </Trigger>
                            <Trigger  Property="IsChecked" Value="False">
                                <Setter  TargetName="border" Property="Background" Value="Green"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>

如果要使用单独的属性来设置切换颜色,则需要遵循附加属性的概念,如其他提到的那样。