<UserControl x:Class="RadGridView_SL5_AR_1.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:my="clr-namespace:RadGridView_SL5_AR_1"
             mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <UserControl.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" 
          Background="White" 
          DataContext="{StaticResource MyViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            
            <DataTemplate x:Key="capacityNotChanged">
                <TextBlock Foreground="Green"
						   Text="{Binding StadiumCapacity, StringFormat='{0:N0}'}" />
            </DataTemplate>
            <DataTemplate x:Key="capacityChanged">
                <TextBlock Foreground="Red"
						   Text="{Binding StadiumCapacity, StringFormat='{0:N0}'}" />
            </DataTemplate>

            <DataTemplate x:Key="establishedYearNotChanged">
                <TextBlock Foreground="Green"
						   Text="{Binding Established, StringFormat='{0:yyyy}'}" />
            </DataTemplate>
            <DataTemplate x:Key="establishedYearChanged">
                <TextBlock Foreground="Red"
						   Text="{Binding Established, StringFormat='{0:yyyy}'}" />
            </DataTemplate>

            <my:ClubDataConverter x:Key="converter"/>
            <my:ConditionalDataTemplateSelector x:Key="stadiumCapacitySelector"
                                                ConditionConverter="{StaticResource converter}">
            
                    <my:ConditionalDataTemplateSelector.Rules>

                    <my:ConditionalDataTemplateRule DataTemplate="{StaticResource capacityNotChanged}">
                        <my:ConditionalDataTemplateRule.Value>
                            <sys:Boolean>False</sys:Boolean>
                        </my:ConditionalDataTemplateRule.Value>
                    </my:ConditionalDataTemplateRule>
                    
                    <my:ConditionalDataTemplateRule DataTemplate="{StaticResource capacityChanged}">
                        <my:ConditionalDataTemplateRule.Value>
                            <sys:Boolean>True</sys:Boolean>
                        </my:ConditionalDataTemplateRule.Value>
                    </my:ConditionalDataTemplateRule>

                </my:ConditionalDataTemplateSelector.Rules>
            </my:ConditionalDataTemplateSelector>
            
            <my:ConditionalDataTemplateSelector x:Key="yearSelector"
                                                ConditionConverter="{StaticResource converter}">
                <my:ConditionalDataTemplateSelector.Rules>

                    <my:ConditionalDataTemplateRule DataTemplate="{StaticResource establishedYearNotChanged}">
                        <my:ConditionalDataTemplateRule.Value>
                            <sys:Boolean>False</sys:Boolean>
                        </my:ConditionalDataTemplateRule.Value>
                    </my:ConditionalDataTemplateRule>

                    <my:ConditionalDataTemplateRule DataTemplate="{StaticResource establishedYearChanged}">
                        <my:ConditionalDataTemplateRule.Value>
                            <sys:Boolean>True</sys:Boolean>
                        </my:ConditionalDataTemplateRule.Value>
                    </my:ConditionalDataTemplateRule>

                </my:ConditionalDataTemplateSelector.Rules>
            </my:ConditionalDataTemplateSelector>
            
        </Grid.Resources>
        <telerik:RadGridView Grid.Row="0" 
                             Name="clubsGrid" 
                             ItemsSource="{Binding Clubs}"
                             AutoGenerateColumns="False"
                             Margin="5">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
                                            Header="Est." 
                                            DataFormatString="{}{0:yyyy}"
                                            CellTemplateSelector="{StaticResource yearSelector}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}" 
                                            Header="Stadium" 
                                            DataFormatString="{}{0:N0}"
                                            CellTemplateSelector="{StaticResource stadiumCapacitySelector}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
       
    </Grid>
   
</UserControl>
