簡體   English   中英

資源字典中的WPF引用樣式和使用觸發器

[英]WPF reference style in resource dictionary and use triggers

我在資源字典中定義了一個適用於所有ComboBox控件的Style ComboBox控件中,我像這樣引用樣式:

Style="{DynamicResource MyComboBoxStyle}"

這沒問題。

我希望能夠為一些ComboBox控件添加一些觸發器。

使用引用為動態資源的Style的好方法是什么,但仍然能夠將Trigger添加到某些ComboBox控件?


更新 :在重新閱讀問題之后,我意識到這並不是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>

我的MainWindow代碼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>

為要應用觸發器的ComboBox控件創建新樣式,並使用新樣式的BasedOn屬性設置其基本樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM