<Page
    x:Class="AllColorsItemsControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AllColorsItemsControl"
    xmlns:ch11="using:Petzold.ProgrammingWindows6.Chapter11"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    
    <Page.Resources>
        <ch11:NamedColor x:Key="namedColor" />
        <ch11:ColorToContrastColorConverter x:Key="colorConverter" />
    </Page.Resources>

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ItemsControl ItemsSource="{Binding Source={StaticResource namedColor},
                                            Path=All}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent},
                                                  Path=Foreground}"
                            BorderThickness="2"
                            Margin="2">
                        <Border.Background>
                            <SolidColorBrush Color="{Binding Color}" />
                        </Border.Background>
                        
                        <Viewbox>
                            <TextBlock Text="{Binding Name}"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center">
                                <TextBlock.Foreground>
                                    <SolidColorBrush Color="{Binding Color,
                                                Converter={StaticResource colorConverter}}" />
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Viewbox>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <ch11:UniformGrid />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Page>
