Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional styling of an element in XAML

I am building a Windows phone 8 app that has a view model property of:

public bool IsReply {get; set;}

In my xaml code, I would like to distinguish two cases:

  1. IsReply=True

    <Grid Margin="0,0,0,0">
    ...
    </Grid>
    
  2. IsReply=False

    <Grid Margin="40,0,0,0">
    ...
    </Grid>
    

Basically, I would like to style the Grid element depending on the value of IsReply. I know that in WPF Style.Triggers exists, but apparently not in WP. The solution I have right now is to have a duplicate copy of the entire grid code and set the visibility of each to a data converter. However, I feel this should be simpler to do.

like image 278
alitaleg Avatar asked Nov 26 '13 02:11

alitaleg


1 Answers

The easiest way is to use a Style with Triggers:

<Grid>
    <Grid.Style>
        <Style TargetType="Grid">
            <Setter Property="Margin" Value="40 0 0 0"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsReply}" Value="True">
                    <Setter Property="Margin" Value="0 0 0 0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>
</Grid>
like image 130
Espen Medbø Avatar answered Oct 13 '22 21:10

Espen Medbø