how to dynamically change rectangle color in Expander?

Deadly 提交于 2019-12-25 03:26:18

问题


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>

Anyone help :


回答1:


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




回答2:


<Rectangle Fill="Transparent" x:Name="ExpanderButtonRectangle"
                   Grid.ColumnSpan="2"/>

ExpanderButtonRectangle.Fill = new SolidColorBrush(Colors.Red);


来源:https://stackoverflow.com/questions/25138707/how-to-dynamically-change-rectangle-color-in-expander

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!