Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Trigger Binding: What's the best way to bind an enum value to visiblity?

I have a user control (NameField). Within it I have a stackpanel containing 3 Grids: StandardView, FluidView, OtherView. Within the code-behind I have a DependencyProperty called View of type NameFieldView (enum). The enum contains STANDARD, FLUID, OTHER.

I think I have to create a converter, but I'm not sure if that's necessary. I basically want to make it so that the only visible grid is the one that matches the enum value... that is, if View = NameFieldView.STANDARD then the Grid named StandardView is visible and the other two are not.

Also, I'm not sure if this should be part of Grid.Resources / Style or Grid.Triggers?

like image 642
myermian Avatar asked Sep 15 '10 17:09

myermian


1 Answers

I use data triggers for this. It looks something like this;

<Style TargetType="DockPanel" x:Key="ViewStyle1">
   <Setter Property="Visibility" Value="Collapsed"/>
   <Style.Triggers>
     <DataTrigger Binding="{Binding ViewStyle}" Value="ViewStyle1">
       <Setter Property="Visibility" Value="Visible"/>
     </DataTrigger>
   </Style.Triggers>
</Style>

Then I create a DockPanel for each view style, and whenever the ViewStyle property changes, the appropriate view displays.

like image 156
Robert Rossney Avatar answered Nov 09 '22 04:11

Robert Rossney