Search code examples
c#wpfxamldata-bindingdatatrigger

Data trigger in static resource


I would like to make a template style, which I can use as a static resource, like this:

<TextBox 
  Style="{StaticResource CollapsingTextBox}"
  Text="{Binding SomeNullableValue}" />

Now as the name suggests, I would like the text box to collapse, when the binding value is null. Normally I would do this with a data trigger, like this:

<DataTrigger Binding="{Binding SomeNullableValue}" Value="{x:null}">
    <Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>

But if I move the data trigger to a template style, which can be reused, then I it's no good to bind directly to the binding property (i.e. SomeNullableValue). Instead I need to bind the the binding of the template user (if that makes sense).

How do I achieve this kind of binding?


Solution

  • try a Trigger with Text property:

    <Trigger Property="Text" Value="">
        <Setter Property="Visibility" Value="Collapsed" />
    </Trigger>
    <Trigger Property="Text" Value="{x:null}">
        <Setter Property="Visibility" Value="Collapsed" />
    </Trigger>