Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: binding, conditional property setter

Tags:

wpf

I have a property that is populated via binding. Color attribute is set the value of Color property

<Rectangle.Fill>
       <SolidColorBrush Color="{Binding Path=Color}"/>
</Rectangle.Fill>

How can I refactor that to the conditional binding like that:

   <Rectangle.Fill>
           <SolidColorBrush Color="{Binding Path=SomeBooleanProperty ? #FF0000 : #00FF00 }"/>
    </Rectangle.Fill>
like image 582
Andrew Florko Avatar asked Jan 05 '11 20:01

Andrew Florko


1 Answers

Thank you, @Jackson, final code is the following:

<Style TargetType="Rectangle" x:Key="ColorBySuccess">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Success}" Value="True">
                    <Setter Property="Fill" Value="#00FF00"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
...
<Rectangle Fill="Green" Style="{StaticResource ColorBySuccess}" .../>
like image 81
Andrew Florko Avatar answered Sep 25 '22 17:09

Andrew Florko