views:

45

answers:

3

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?

+4  A: 

Like a lot of WPF, it really depends on your taste. Here are a few choices.

You could create three IValueConverter that converts the value of the View property to a Visibility (or use the enum name as a ConverterParameter and create one converter).

You could create three new properties called "StandardViewIsVisible", "FluidViewIsVisible", and "OtherViewIsVisible" that get updated when the View property changes. These properties would be of return type Visibility. This is decidedly more of an "MVVM" way of doing things, even if you aren't using a ViewModel.

You could use a DataTrigger that sets the appropriate grid Visible or Collapsed based on the current value of the View property.

Wonko the Sane
I actually went with one ValueConverter and used a parameter to decide which view to check against. 'Fluid' checks to see if View is set to Fluid. 'Other' checked to see if View was either Standard or Extended. And within that grid 2 combobox items passed in 'Extended' to see if View was Extended. Yay!
myermian
A: 

I would create a converter. If you add a converter whenever you have an appropriate binding problem like this, you will slowly build a library of them for your application, making things much easier for yourself in the future. I would call it something like NameFieldViewToVisibilityConverter - it should have two methods:

public Object Convert(Object value, Type TargetType, Object param, CultureInfo Culture);
public Object ConvertBack(Object value, Type TargetType, Object param, CultureInfo Culture);

Convert will have a NameFieldView param and returns a Visibility value. ConvertBack will have a Visibility param and returns a NameFieldView value.

The bindings would look like this:

<Grid Name="StandardView" Visibility="{Binding View, Converter={StaticResource NameFieldViewToVisibilityConverter}"  />
Khyad Halda
A: 

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.

Robert Rossney