views:

2001

answers:

2

I have a tab page that should be hidden if a property (BlahType) is set to 1 and shown if set to 0. This is what I WANT to do:

<TabItem Header="Blah">
    <TabItem.Triggers>
     <DataTrigger Binding="{Binding BlahType}" Value="0">
      <Setter Property="TabItem.Visibility" Value="Hidden" />
     </DataTrigger>
    </TabItem.Triggers>
</TabItem>

The problem is, I get this error:

"Triggers collection members must be of type EventTrigger"

If you Google that error, you'll see that Dr. WPF explains the error. Is there a clean way to do what I'm trying to achieve here?

A: 

Well, you can't do that using triggers (not unless you are inside a DataTemplate, ControlTemplate or a Style).

You can do it using a binding, you will need to write a custom ValueConverter that will translate between 0/1 to Visible/Hidden and then you can bind the Visibility property to the BlhaType property.

Or you can just set the Visibility in code and give up on a XAML based approach (that what I would have done).

Nir
+5  A: 

I believe that the Triggers collection of a control only currently supports EventTriggers. If you would like to use a DataTrigger simply place it inside a style, for your example:

<TabItem Header="Blah">
    <TabItem.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding BlahType}" Value="0">
                    <Setter Property="TabItem.Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabItem.Style>
</TabItem>
David Padbury
You have a slight typo... "<Style.Triggers>" should be "</Style.Triggers>"... this is great, thanks!
Timothy Khouri
Right you are, thanks for picking that up!
David Padbury