【问题标题】:WPF reference style in resource dictionary and use triggers资源字典中的 WPF 引用样式并使用触发器
【发布时间】:2011-02-09 18:02:28
【问题描述】:

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

Style="{DynamicResource MyComboBoxStyle}"

这没问题。

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

将Style 引用为动态资源但仍能将Triggers 添加到某些ComboBox 控件的好方法是什么?

【问题讨论】:

    标签: wpf triggers styles resourcedictionary


    【解决方案1】:

    更新: 重新阅读问题后,我意识到这并不是 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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 尝试执行此操作时出现以下错误:无法在“Style”类型的“BasedOn”属性上设置“DynamicResourceExtension”。
      • 为什么要使用 DynamicResource? StaticResource 应该适用于设置样式。
      • 样式位于一个单独的模块中(使用 CAL),所以我必须指定 DynamicResource 才能使用它。
      • 所以基本上这个问题还没有解决……我也想要一个答案。
      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多