【发布时间】:2014-09-05 06:15:36
【问题描述】:
我在以编程方式更改 TreeView 中的切换颜色时遇到问题。
我在 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 样式中应用它一定有问题。
【问题讨论】:
-
您的问题似乎与这里提出的问题非常相似:stackoverflow.com/questions/15819796/… 答案和讨论可能对您有用。
-
谢谢提示,我会检查一下..
-
您可以在this web page 找到
ToggleButton的默认ControlTemplate。或者,如果这没有帮助,也许您可以使用绑定或事件处理程序来实现您想要的?尝试从另一个方向看它。您的总体目标是什么? -
我的目标是:用户更改切换按钮的背景颜色。 :] 但我“解决”它没有切换按钮,这是我认为最好的解决方案:D
标签: c# wpf treeview wpf-controls togglebutton