[英]Change color of toggle (TreeView) in wpf programmatically
我在以編程方式更改TreeView中Toggle的顏色時遇到問題。 我在ResourceDictionary SolidColorBrush中定義了:
<SolidColorBrush x:Key="FillBrush" Color="#000000" />
我也在ResourceDictionary中為toggleButton創建了樣式:
<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid Width="auto" Height="auto" Background="Transparent" ShowGridLines="True">
<Path Fill="{StaticResource FillBrush}" x:Name="ExpandPath" HorizontalAlignment="Center" VerticalAlignment="Center"
Data="M 0 0 L 0 2 L 6 0 z M 6 2 L 6 0 L 0 2 z">
</Path>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="ExpandPath" Value="M 0 0 L 0 8 L 2 0 z M 2 8 L 2 0 L 0 8 z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在代碼中,我嘗試以編程方式更改FillBrush鍵的值:
SolidColorBrush cc = (SolidColorBrush)Resources["FillBrush"];
cc.Color=c.Color;
但是當我更改它時,“切換”按鈕不會更改。
有什么提示如何更改現有組件的顏色? 我對通過ResourceDictionary模板化組件有些困惑,所以我猜那里有些問題。 感謝所有幫助
編輯:在代碼中,我正在“說”使用StaticResource。 我也使用DynamicResource進行了嘗試,但是什么也沒發生,並且顏色值沒有改變。
編輯2:如果我用以下方法檢查FillBrush資源中的值:
MessageBox.Show(((SolidColorBrush)Resources["FillBrush"]).Color.ToString());
該值將替換為新值。 這樣就行了。 在這種情況下,以ToggleButton樣式應用它一定有問題。
StaticResource
將僅讀取初始顏色。 為了動態更新顏色,您需要將Brush
聲明為DynamicResource
。
<Path Fill="{DynamicResource FillBrush}" x:Name="ExpandPath" HorizontalAlignment="Center"
VerticalAlignment="Center" Data="M 0 0 L 0 2 L 6 0 z M 6 2 L 6 0 L 0 2 z">
這篇文章中有一個例子。
嘗試更換Brush
-不更改其Color
,例如。
this.Resources["FillBrush"] = new SolidColorBrush(Colors.Red);
即Brush
可能被Freezed
。 例如,檢查此鏈接(僅第一個) Style Setter冷凍刷
如果您執行以下操作...
if( !brush.IsFrozen ) // 'not froze' i.e. free to change
brush.Color = Colors.Red;
...機會永遠不會進入。 如果訪問凍結的對象,也會得到異常。
或者更好的是,您應該...
var brush = (SolidColorBrush)this.Resources["FillBrush"];
if (brush.IsFrozen)
{
SolidColorBrush clone = brush.Clone();
clone.Color = Colors.Red;
this.Resources["FillBrush"] = clone;
}
else
brush.Color = Colors.Red;
有關更多詳細信息,請檢查此主題上的第一個MS鏈接,該鏈接完全涉及這些問題...
使用Blend,我只需不到5次鼠標單擊即可完成此操作,
(要查看完整尺寸的圖像,請右鍵單擊它,然后選擇“查看圖像”或“在新選項卡中打開圖像”,這取決於您使用的Web瀏覽器)
我沒有粘貼生成的XAML,因為有很多內容,只是Blends突出顯示的部分。
有時,如果您要更改的項目以某種樣式“埋沒”,則在嘗試對其進行更改時似乎並沒有得到認可,至少這是我之前嘗試對樣式進行樣式設置並希望能夠動態更改顏色或字體系列。 嘗試這個:
<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Foreground" Value="{StaticResource FillBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid Width="Auto" Height="Auto" Background="Transparent" ShowGridLines="True">
<Path Fill="{Binding Foreground}" x:Name="ExpandPath" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 0 2 L 6 0 z M 6 2 L 6 0 L 0 2 z"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
並且當您想要更改切換按鈕的顏色時-而不是更改FillBrush =您可以更改實際ToggleButton的前景值。 由於“路徑填充”綁定到前景色,因此應觸發它進行更改。
希望這對您有所幫助,並且對您有用,我承認我尚未測試過此特定的XAML代碼段,但在以前的項目中必須做類似的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.