WPF在资源字典中引用样式并使用触发器

发布于 2024-08-30 03:33:24 字数 344 浏览 6 评论 0原文

我在资源字典中定义了一个 Style ,适用于 所有ComboBox 控件。在 ComboBox 控件中,我像这样引用样式:

Style="{DynamicResource MyComboBoxStyle}"

这工作正常。

我希望能够向某些 ComboBox 控件添加一些触发器。

使用作为动态资源引用的 Style 而又仍然能够将 Trigger 添加到某些 ComboBox 控件的好方法是什么?

I have a Style defined in a resource dictionary that applies to
all ComboBox controls. Within the ComboBox control, I reference the style like so:

Style="{DynamicResource MyComboBoxStyle}"

This works ok.

I want to be able to add some triggers to some of the ComboBox controls.

What is a good way to use the Style referenced as a dynamic resource yet still be able to add Triggers to some of the ComboBox controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-09-06 03:33:24

更新
重新阅读问题后,我意识到这并不是OP所问的问题。我可以删除它,但也许它对偶然发现这个问题的人有用。


下面是一个示例,其中包含定义模板和触发器的 xaml 资源字典,以及引用该资源并应用样式的窗口。

它可能会帮助某人研究使用模板和触发器:

我的资源名为“Style1.xaml”

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TonyTemplate" TargetType="Button">
    <Border Name="Border" 
            BorderBrush="Orange" 
            BorderThickness="3" 
            CornerRadius="2" 
            Background="Ivory" 
            TextBlock.Foreground="Black">
        <Grid>
            <ContentPresenter RecognizesAccessKey="True" 
                              Margin="{TemplateBinding Padding}"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="Border" Property="Background" Value="Yellow" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter TargetName="Border" Property="Background" Value="Chartreuse" />
            <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

我的主窗口代码 xaml:

<Window x:Class="WpfApplication1.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>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button Width="100" Height="50" 
                Template="{StaticResource TonyTemplate}" 
                Content="Click me"/>
    </Grid>
</Window>

Update:
After re-reading the question, I realize this isn't exactly what the OP was asking about. I could delete this, but perhaps it will be useful to someone who stumbles upon this question.


Here's an example, with a xaml resource dictionary defining the template and the triggers, along with a window that references that resource and applies the style.

It may help someone looking into using templates and triggers:

My resource named "Style1.xaml"

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TonyTemplate" TargetType="Button">
    <Border Name="Border" 
            BorderBrush="Orange" 
            BorderThickness="3" 
            CornerRadius="2" 
            Background="Ivory" 
            TextBlock.Foreground="Black">
        <Grid>
            <ContentPresenter RecognizesAccessKey="True" 
                              Margin="{TemplateBinding Padding}"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="Border" Property="Background" Value="Yellow" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter TargetName="Border" Property="Background" Value="Chartreuse" />
            <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

My MainWindow Code xaml:

<Window x:Class="WpfApplication1.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>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button Width="100" Height="50" 
                Template="{StaticResource TonyTemplate}" 
                Content="Click me"/>
    </Grid>
</Window>
红衣飘飘貌似仙 2024-09-06 03:33:24

为要应用触发器的 ComboBox 控件创建新样式,并使用 BasedOn 新样式上的属性来设置其基本样式。

Create new styles for the ComboBox controls that you want to apply triggers to, and use the BasedOn property on the new style to set their base style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文