Timothy Khouri
Timothy Khouri

Reputation: 31845

How can I selectively set the "Visibility" of a TabItem via DataBinding/Triggers

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?

Upvotes: 6

Views: 6685

Answers (2)

David Padbury
David Padbury

Reputation: 1182

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>

Upvotes: 13

Nir
Nir

Reputation: 29584

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).

Upvotes: 0

Related Questions