0

我有以下风格(为简洁起见),并有一些基于它的问题。据我了解,如果 ControlTemplate 替换了 Style 所基于的控件的整个可视化树,那么 Setters 属性会产生什么影响?

在此示例中,FontSize、Margin、Height 等的属性 Setters 是否与 CheckBox 本身的相应属性相对应?如果替换控件的 Template 属性,如果 CheckBox 不再呈现其默认外观,这些 Setter 将对应什么?

<Style x:Key="KeyName" TargetType="CheckBox">
    <Setter Property="FontSize" Value="11" />
    <Setter Property="Margin" Value="0 0 1 0" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Height" Value="18" />
    ... common property setters etc.

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <Border>
                    <StackPanel>
                        <Ellipse Name="Ellipse" Width="7" Height="7" />
                        <ContentPresenter Content="{TemplateBinding Content}" />
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.Setters>
                            <Setter Property="Foreground" Value="WhiteSmoke" />
                        </Trigger.Setters>
                    </Trigger>
                        ... custom triggers etc ...
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

1 回答 1

1

它们是一种为正在设置样式的对象的属性提供初始默认值的方法,它们不会为您自动强制模板上的任何内容。但是,它们可以在控制模板中使用。

使用样式中的设置器设置的值可以被 xaml 中的本地值覆盖。例如。

这个 xaml 文件绘制了一个标签,该标签的样式已更改为包含一个采用背景颜色的网格,我在 setter 中将颜色默认为红色,它显示为红色。

<Window x:Class="ContextMenu.MainWindow"      
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Grid Background="{TemplateBinding Background}">
                            <TextBlock Text="{TemplateBinding Content}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>
    </Window.Resources>

    <Label>Test</Label>
</Window>

如果我要将标签实例上的标签线更改为蓝色,您可以看到这会覆盖设置器。

<Window x:Class="ContextMenu.MainWindow"      
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Grid Background="{TemplateBinding Background}">
                            <TextBlock Text="{TemplateBinding Content}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>
    </Window.Resources>

    <Label Background="Blue">Test</Label>
</Window>
于 2012-11-06T16:43:35.010 回答