如何在Expander中动态更改矩形颜色? [英] how to dynamically change rectangle color in Expander?
本文介绍了如何在Expander中动态更改矩形颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个扩展器,其中的矩形带有颜色.我想在某些功能后进行动态更改.如何在运行时更改它?
I have an Expander in which i have Rectangle with color. i want to change dynamically after some functionality. how to change it on run time ?
<Windows.Resources>
<ControlTemplate x:Key="SimpleExpanderButtonTemp"
TargetType="{x:Type ToggleButton}">
<Border x:Name="ExpanderButtonBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle x:Name="ExpandRect" Fill="Transparent"
Grid.ColumnSpan="2"/>
<Ellipse Name="Circle"
Grid.Column="0"
Stroke="DarkGray"
Width="20"
Height="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</Grid>
</Border>
</>
</Windows.Resources>
任何人的帮助:
推荐答案
如果要在后面的代码中进行更改,我将使用{DynamicResource key}绑定绑定到画笔资源.然后,您后面的代码可以将该资源更新为所需的任何位置.
If you want to change this in code behind, I would bind to a brush resource using {DynamicResource key} binding. Your code behind can then update that resource to whatever you want where ever you need it to.
this.Resource["RectangleBrush"] = new SolidColorBrush(Colors.Red);
If you want to control it via data in a view model, use a datatrigger. Link to similar answer
If you're basing it on multiple bindings - you can use a multibinding with a converter : Tutorial here
这篇关于如何在Expander中动态更改矩形颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文