Reputation: 883
I have rectangle which I want to fill with different pictures based on property. I created a binding to bool variable and styles. But code does not work.
Error: Cannot convert ystem.Windows.Media.Imaging.BitmapImage to System.Windows.Media.Brush
<Rectangle Width="20" Height="20" Canvas.Left="{Binding x}" Canvas.Top="{Binding y}">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding teamgreen}" Value="True">
<Setter Property="Fill" Value="{StaticResource tankGreen}" />
</DataTrigger>
<DataTrigger Binding="{Binding teamgreen}" Value="False">
<Setter Property="Fill" Value="{StaticResource tankBlue}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
tankGreen and tankBlue look like this
<BitmapImage x:Key="tankBlue" UriSource="..\Images\tankBlue.png" />
<BitmapImage x:Key="tankGreen" UriSource="..\Images\tankGreen.png" />
they work fine when I use
<Rectangle.Fill>
<ImageBrush ImageSource="{DynamicResource tankBlue}" />
</Rectangle.Fill>
I guess I just dont know how to incorporate ImageBrush into style for datatrigger setter
Upvotes: 1
Views: 1990
Reputation: 5514
You can set Setter.Value
just like you set Rectangle.Fill
:
<DataTrigger Binding="{Binding teamgreen}" Value="True">
<Setter Property="Fill">
<Setter.Value>
<ImageBrush ImageSource="{DynamicResource tankGreen}" />
</Setter.Value>
</Setter>
</DataTrigger>
Upvotes: 2