PigSpider
PigSpider

Reputation: 1011

WPF treeview item visibility binding to property + static variable

I have a WPF treeview, with nodes visibility dependent on their "Deleted" binding property:

    <Style x:Name="SelectedTreeItem" TargetType="TreeViewItem">
    <EventSetter Event="MouseRightButtonDown" Handler="TreeSetup_MouseRightButtonDown"/>
          <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
          <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
          <Setter Property="Visibility" Value="Visible" />
          <Style.Triggers>
            <DataTrigger Binding="{Binding Deleted}" Value="True">
               <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
          </Style.Triggers>
   </Style>

I would like to make add some way to ignore this deleted property value. This would be achieved by a button "Show deleted items". I do not want to change the "Deleted" value, but rather add a global condition to that forces this binding to ignore untill the button is pressed again.

Upvotes: 0

Views: 990

Answers (2)

PigSpider
PigSpider

Reputation: 1011

I used the other answer in conjuction with a static resource

<Style>
        <Setter Property="Control.Visibility" Value="Visible" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=IsPropA}" Value="false" />
                    <Condition Binding="{Binding Source={StaticResource ClassName},Path=PropertyName}" Value="False"/>                                    
                </MultiDataTrigger.Conditions>
                <Setter Property="Control.Visibility" Value="Hidden" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

Just had to create the local resource under Window resources:

    <Window.Resources>
    <local:ClassName x:Key="ClassName"/>

And then the button used to show deleted or not:

     private void btnApply_Click(object sender, RoutedEventArgs e)
    {
        //ApplyGeneralChanges();
        var item = this.FindResource("ClassName") as ClassName;
        item.DisplayDeleted = !item.DisplayDeleted;
    }

Upvotes: 0

manton
manton

Reputation: 171

You can do that via MultiDataTrigger.Conditions. Here is an example for visibility:

<Style>
            <Setter Property="Control.Visibility" Value="Visible" />
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Path=IsPropA}" Value="false" />
                        <Condition Binding="{Binding Path=IsPropB}" Value="false" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Control.Visibility" Value="Hidden" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>

Upvotes: 2

Related Questions