[英]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.