첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ 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
TAG , ,

댓글을 달아 주세요