Conditional styling of an element in XAML

c#, windows-phone-8, xaml

Solution

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>

Problem

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: `IsReply=True` ``` <Grid Margin="0,0,0,0"> ... </Grid> ``` `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.

Original source