[英]How can I apply style based on a trigger property to a content control?
在當前正在使用的WPF應用程序中,當鼠標被按下時(屬性isPressed = true),我必須反轉圖標的顏色。 我需要這樣做的原因是我想保持應用程序在圖標顏色和背景方面的一致性。 但是此圖標嵌入在按鈕內,單擊該按鈕時會顯示類似的顏色,因此該圖標不可見
當我的樣式觸發器拾取我設置的紅色時,但幾何圖形的顏色未更改。 (isPressed = true)時,有什么方法可以使用此樣式觸發器將幾何圖形的顏色也設置為紅色?
這是我的代碼
我的風格如下
<Style x:Key="ButtonBackground" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="Content" Value="yellow" />
</Trigger>
</Style.Triggers>
</Style>
我嘗試將setter屬性設置為內容為黃色,只是為了查看它是否有效但不起作用
使用它的部分如下
<Border BorderBrush="{StaticResource ContentBackground}" BorderThickness="0,0,0,1">
<Button
x:Name="NameButton"
Style="{StaticResource ButtonBackground}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal">
<ContentControl x:Name="Icon"
Width="25"
Height="25"
">
</ContentControl>
<TextBlock Text="{Binding Name}"
"/>
</StackPanel>
</Grid>
</Button>
</Border>
如圖像中所示,樣式由文本塊拾取,但是我不知道如何將這種樣式添加到具有圖標的contentcontrol中
編輯 :-在應用程序中指定的圖標示例
<DataTemplate x:Key="FolderIcon">
<Path
Fill="#047F89"
Data="....."/>
</DataTemplate>
您必須將Path
的Fill
屬性綁定到父Control
元素的Control.Foreground
(或Button.Foreground
),而不是直接設置它。 現在,圖標顏色將適應觸發器分配的Foreground
值。 我還刪除了Button.Content
的冗余Grid
和Trigger
的Content
Setter
,因為每次按下Button
都會刪除圖標Path
元素,並將默認的Forground
值添加到Style(從Path.Fill
為Style-塞特)
<Style x:Key="ButtonBackground" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="#047F89" />
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
<Button x:Name="NameButton"
Style="{StaticResource ButtonBackground}">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Path Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Control}, Path=Foreground}"
Height="16"
Width="16"
Stretch="Uniform"
VerticalAlignment="Center"
Margin="5,0,0,0"
Data="....."/>
<TextBlock Text="{Binding Name}"
TextTrimming="WordEllipsis"
VerticalAlignment="Center"
Margin="5,0,0,0"/>
<StackPanel>
<Button.Content>
</Button>
萬一您決定將Path
移至ResourceDictionary
,則Path.Fill
上的Binding
仍然有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.