Quantcast
Channel: Grid Background Colour Based on Item Property - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by Romasz for Grid Background Colour Based on Item Property

$
0
0

You can for example use a Converter for this task:

Define a converter class in a namespace:

namespace MyConverters
{
  public class BoolToBrush : IValueConverter
  {
    private Brush FalseValue = new SolidColorBrush(Colors.Gray);
    public Brush TrueValue = Application.Current.Resources["PhoneAccentBrush"] as Brush;

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null) return FalseValue;
        else return (bool)value ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value != null ? value.Equals(TrueValue) : false;
    }
  }
}

Then in your XAML in Page.Resources define a key (don't forget to add namespace):

<Page ...
      ... some code ...
 xmlns:converter="MyConverters"
      .../>
<Page.Resources>
    <converter:BoolToBrush x:Key="BoolToBrush"/>
</Page.Resources>

Then finally you can use your converter with Binding:

<PivotItem x:Uid="PivotBlocks" Margin="10, 10, 10, 10" Header="blockx" DataContext="{Binding Blocks}" d:DataContext="{Binding , Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:DataSource}}">
    <GridView ItemsSource="{Binding Formations}" IsItemClickEnabled="True" ItemClick="Point_ItemClick" Loaded="PivotBlocks_Loaded" ContinuumNavigationTransitionInfo.ExitElementContainer="True">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid Width="80" Height="80" Margin="0,0,10,10" Background="{Binding HasBeenSelected, Converter={StaticResource BoolToBrush}}">
                    <StackPanel VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding Shorthand}" Padding="5, 0, 0, 5" Style="{StaticResource SubheaderTextBlockStyle}" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
</PivotItem>

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>