WPF

[ wpf ] 리스트뷰에 바인딩된 데이터 색상 변환 IValueConverter

code094 2023. 1. 3. 12:57

1. converter 클래스 파일 생성

2. xaml 에서 변환기 호출 

csharp 성적 별로 다른 색상으로 변경 

xaml

<Window x:Class="WpfApp6.MainWindow"
        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:local="clr-namespace:WpfApp6"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:ColorConverter x:Key="ScoreColorConverter"/>
        
        <DataTemplate x:Key="CellTemplateConverter">
            <TextBlock Foreground="{Binding Path= csharp, Converter={StaticResource ScoreColorConverter}}"
                       Text="{Binding Path= csharp}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="1" Grid.ColumnSpan="3" Grid.RowSpan="2" Margin="5" Name="sListView">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn DisplayMemberBinding="{Binding name}" Header="name" Width="50"/>
                        <GridViewColumn DisplayMemberBinding="{Binding age}" Header="age" Width="50"/>
                        <GridViewColumn DisplayMemberBinding="{Binding java}" Header="java" Width="50"/>
                        <GridViewColumn CellTemplate="{StaticResource CellTemplateConverter}" 
                                        Header="csharp" Width="50"/>
                        <GridViewColumn DisplayMemberBinding="{Binding python}" Header="python" Width="50"/>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

c#

public class Student
    {
        public string name { get; set; }
        public int java { get; set; }

        public int csharp { get; set; }
        public int python { get; set; }
        public int age { get; set; }
    }
public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush color = new SolidColorBrush(Colors.White);

            int data = (int)value;
            if (data < 30)
            {
                color = new SolidColorBrush(Colors.Red);
            }
            else if (data < 60)
            {
                color = new SolidColorBrush(Colors.Blue);
            }
            else if (data < 80)
            {
                color = new SolidColorBrush(Colors.BurlyWood);
            }
            else if (data < 100)
            {
                color = new SolidColorBrush(Colors.DimGray);
            }
            else if (data == 100)
            {
                color = new SolidColorBrush(Colors.Aqua);
            }

            return color;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
public MainWindow()
        {
            List<Student> students = null;
            
                InitializeComponent();

                students = new List<Student>();
                students.Add(new Student() { name = "jin", csharp = 100, java = 60, python = 70, age = 20 });
                students.Add(new Student() { name = "sin", csharp = 40, java = 30, python = 70, age = 20 });
                students.Add(new Student() { name = "young", csharp = 30, java = 80, python = 65, age = 20 });
                students.Add(new Student() { name = "kim", csharp = 65, java = 70, python = 70, age = 20 });
                students.Add(new Student() { name = "song", csharp = 90, java = 60, python = 100, age = 20 });

                sListView.ItemsSource = students;

            }
        }