WPF样式基于无法正常工作 [英] WPF Style BasedOn does not work

查看:96
本文介绍了WPF样式基于无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了如下样式的RadioButtonToggleButtonStyle.

I have made a style RadioButtonToggleButtonStyle as below.

  <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                        d1p1:Ignorable="d" 
                        xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"                    
                        xmlns:local="clr-namespace:TeachpendantControl">

        <Style x:Key="RadioButtonToggleButtonStyle" 
               BasedOn="{StaticResource {x:Type ToggleButton}}"
               TargetType="{x:Type RadioButton}">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="Gold"/>
                </Trigger>
            </Style.Triggers>
        </Style>   
    </ResourceDictionary>

我在下面这样使用它.

 <ListBox  Grid.Column="0" Grid.Row="0" Grid.RowSpan="10" ItemsSource="{Binding LeftPaneViewModelInfoItems, UpdateSourceTrigger=PropertyChanged}" Background="Transparent" SelectedItem="{Binding SelectedViewModelInfoItem}">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                        <ContentPresenter />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <RadioButton 
                                Content="{Binding Text}"                                     
                                GroupName="DisplayPage"
                                IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                                Style="{StaticResource RadioButtonToggleButtonStyle}"                                     
                                >                                    
                            </RadioButton>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

当我运行应用程序时,CheckBoxes显示为ToggleButtons(但在设计模式下显示为CheckBoxes).

The CheckBoxes show up as ToggleButtons when I run the application (but it shows up as CheckBoxes in Design mode).

但我没有得到一个金色背景当(如样式<4>)被选中.此外,Visual Studio(2015)在资源文件中抱怨说资源{x:Type ToggleButton}" 无法解析.

But I don't get a gold background when the CheckBox (styled as ToggleButton) is checked. Also Visual Studio (2015) complains in the resource file saying "The resource {x:Type ToggleButton}" could not be resolved.

任何人都可以解释为什么它不起作用(以及如何解决)

Can anyone provide an explanation of why this does not work (and how to solve it)

推荐答案

您需要修改ControlTemplate才能更改ToggleButtonBackground.

You need to modify the ControlTemplate to be able to change the Background of a ToggleButton.

默认值ControlTemplate包含优先于您在Style中指​​定的值的硬编码值.

The default ControlTemplate contains hardcoded values that take precedence over the value that you specify in your Style.

您可以通过在Visual Studio或Blend中的设计模式中右键单击ToggleButton并将默认模板复制到项目中,然后选择编辑模板"->编辑副本",然后根据需要对其进行编辑.试试这个:

You can copy the default template into your project by right-clicking on a ToggleButton in design mode in Visual Studio or in Blend and choose Edit Template->Edit a Copy and then edit it as per your requirements. Try this:

<Style x:Key="RadioButtonToggleButtonStyle" TargetType="{x:Type ToggleButton}">
    <Style.Resources>
        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
        <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
        <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
        <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
        <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
        <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
        <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
        <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
        <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
    </Style.Resources>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="Button.IsDefaulted" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter Property="Background" TargetName="border" Value="Gold" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style> 

这篇关于WPF样式基于无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆