Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set DynamicResource as a TargetNullValue?

Tags:

wpf

If not, how can I achieve a similiar behaviour? I want to bind the Backgrond property of some control to a property of type Brush. I want to use the default background (which is resolved using a resource key) when that property is set to null.

like image 679
milosz Avatar asked Dec 17 '25 17:12

milosz


1 Answers

Default implementation of TargetNullValue does not allow that. One of possible simple solutions can be implementing a Style with DataTrigger:

 <Style TargetType="TextBlock">
     <Setter Property="Background" Value="{Binding BrushProperty}" />
     <Style.Triggers>
         <DataTrigger Binding="{Binding BrushProperty}" Value="{x:Null}">
              <Setter Property="Background" Value="{DynamicResource defaultBrush}" />
         </DataTrigger>
     </Style.Triggers>
 </Style>

I used TextBlock type just as example, you can use the type you need there.

like image 121
icebat Avatar answered Dec 20 '25 00:12

icebat