첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ TreeView 엘리먼트에서 AlternationConverter 객체를 사용해 트리 뷰 항목의 배경색/폰트 스타일을 설정하는 방법을 보여준다.

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="TreeView 엘리먼트 : AlternationConverter 객체를 사용해 트리 뷰 항목 배경색/폰트 스타일 설정하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:LeagueList x:Key="LeagueListKey" />
        <AlternationConverter x:Key="TeamBackgroundAlternationConverterKey">
            <SolidColorBrush>LimeGreen</SolidColorBrush>
            <SolidColorBrush>SpringGreen</SolidColorBrush>
            <SolidColorBrush>Chartreuse</SolidColorBrush>
        </AlternationConverter>
        <AlternationConverter x:Key="DivisionFontStyleAlternationConverterKey">
            <FontStyle >Italic</FontStyle>
            <FontStyle >Normal</FontStyle>
        </AlternationConverter>
        <DataTemplate x:Key="TeamDataTemplateKey">
            <TextBlock
                Text="{Binding Path=Name}"
                Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}},
                    Path=(ItemsControl.AlternationIndex),
                    Converter={StaticResource TeamBackgroundAlternationConverterKey}}" />
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="DivisionHierarchicalDataTemplateKey"
            ItemTemplate="{StaticResource TeamDataTemplateKey}"
            ItemsSource="{Binding Path=TeamList}"
            AlternationCount="3">
            <TextBlock
                Text="{Binding Path=Name}"
                FontStyle="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}},
                    Path=(ItemsControl.AlternationIndex),
                    Converter={StaticResource DivisionFontStyleAlternationConverterKey}}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="LeagueHierarchicalDataTemplateKey"
            ItemTemplate="{StaticResource DivisionHierarchicalDataTemplateKey}"
            ItemsSource="{Binding Path=DivisionList}">
            <TextBlock
                FontWeight="Bold"
                Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <Style TargetType="TreeViewItem">
            <Setter Property="Padding"    Value="1"    />
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </Window.Resources>
    <Grid Margin="10">
        <TreeView
            ItemTemplate="{StaticResource LeagueHierarchicalDataTemplateKey}"
            ItemsSource="{Binding Source={StaticResource LeagueListKey}}" />
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요