views:

56

answers:

2

I'm trying to set a button's event to change a grid's visibility using just XAML. I'm pretty sure I should be using an EventTrigger, but I couldn't get that to work so my most recent attempt is to create a DataTrigger that is bound to a field in the view-model:

<Style x:Key="PersonalInfoGridStyle" TargetType="{x:Type Grid}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=PersonalInfoGridVisibility}" Value="Collapsed">
            <Setter Property="Grid.Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

The above code isn't working, but that matters less to me that achieving the whole thing in just XAML (without using the view-model or code-behind).

Can someone explain how I should go about this or if it's even possible? :) THANKS AHEAD

+4  A: 

Can you use a toggle button?

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="boolConverter" />
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ToggleButton Content="Click Me" x:Name="toggleMe" Grid.Row="0" />
        <Grid Grid.Row="1" Background="Black" Visibility="{Binding ElementName=toggleMe, Path=IsChecked, Converter={StaticResource boolConverter}}">

        </Grid>
    </Grid>
</Window>
rudigrobler
Thanks for the answer! A toggle button could work but there are multiple grids that will have their visibility changed, so triggers give me the flexibility to use multiple setter statements.
Scifiballer24
you can also bind the other grids visibility to the togglebutton.
dnr3
A: 

Okay I figured out how I'm going to do this (using the view-model). I created an object in the view-model of type Visibility (NOT STRING), then I used the Binding attribute in the Grid to bind the Visibility attribute in the grid to the Visibility object in the view-model.

Now all I needed to do was have the button's event change the visibility object (ONE LINE!) to visible like so:

<Grid Visibility="{Binding Path=GridVisibility">
    [content here]
</Grid>

And in the view model:

private Visibility _gridVisibility = Visibility.Visible;
public Visibility GridVisibility
{
    get
    {
        return _gridVisibility;
    }
}

Use your MVVM method to set the property yourself (I left that out purposefully).

And finally just bind you button's click event to a method that simply changes the value of GridVisibility to Visibility.Hidden or Visibility.Collapsed.

Scifiballer24
Whilst this solution does work you don't really want to add property types that are too specific too your viewmodel (such as visibility) instead you should bind to a boolean property and use a booleanToVisibility converter. This way your logic in your viewmodel can set the boolean rather than setting the visibility. However if it is very view specific then you should probably go with rudigroblers answer of using triggers
mattythomas2000
The question also said without a ViewModel?
rudigrobler
In which case I would certainly recommend your response as the answer...
mattythomas2000
Value converters in general are unnecessary if you're using MVVM; the whole point of view models is to provide data to the view in a form that's easy for the view to consume. There's no reason in the world not to use the `Visibility` type in a view model. The view model is just as testable, and just as decoupled from the implementation details of the view, as it would be if the property were boolean.
Robert Rossney