C# WPF, 지난번 포스트 소스에 연결됩니다.
상황에 따라, 표시되는 템플릿이 자동으로 지정되게끔 만들 수 있네요.
SubjectSelectDataTemplate.cs
using System.Windows;
using System.Windows.Controls;
namespace WPF_MVVM_SAMPLE01
{
public class SubjectSelectDataTemplate : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
Score score = item as Score;
FrameworkElement el = container as FrameworkElement;
return (DataTemplate)el.FindResource(score.SCORE > 60 ? "DataTemplate3" : "DataTemplate4");
}
}
}
TemplateSelectWindow.xaml
<Window x:Class="WpfApp1.TemplateSelectWindow"
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:WPF_MVVM_SAMPLE01"
mc:Ignorable="d"
Title="TemplateSelectWindow" Height="450" Width="800">
<Window.DataContext>
<local:ItemViewModel></local:ItemViewModel>
</Window.DataContext>
<Window.Resources>
<local:SubjectSelectDataTemplate x:Key="SubjectSelectDataTemplate"></local:SubjectSelectDataTemplate>
</Window.Resources>
<DockPanel>
<ListBox ItemsSource="{Binding Items}" ItemTemplateSelector="{StaticResource SubjectSelectDataTemplate}" ></ListBox>
</DockPanel>
</Window>
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_MVVM_SAMPLE01">
<DataTemplate x:Key="DataTemplate1">
<TextBlock Text="{Binding SUBJECT}"></TextBlock>
</DataTemplate>
<DataTemplate x:Key="DataTemplate2">
<Grid >
<Rectangle HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>
<TextBlock Text="{Binding SCORE}"></TextBlock>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DataTemplate3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding SUBJECT}"></TextBlock>
<Rectangle Grid.Column="1" HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>
<TextBlock Grid.Column="1" Text="{Binding SCORE}"></TextBlock>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DataTemplate4">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{Binding SUBJECT}"></TextBlock>
<Rectangle Grid.Column="0" HorizontalAlignment="Left" Height="30" Width="{Binding SCORE}" StrokeThickness="1" Fill="Red"></Rectangle>
<TextBlock Grid.Column="0" Text="{Binding SCORE}"></TextBlock>
</Grid>
</DataTemplate>
</ResourceDictionary>
소스는 여기 올려뒀습니다. https://github.com/erith/WPF_SAMPLES/tree/master/WpfApp1
'C#' 카테고리의 다른 글
WPF RichText에서 HTML 표시하기 (2) | 2019.02.09 |
---|---|
Visual Studio Debug.WriteLine 표시가 안될때 대처 방안 (2) | 2018.11.15 |
WPF MVVM 샘플코드 (0) | 2018.05.28 |
doxygen 설치 및 설정 (0) | 2018.05.14 |
간단한 한글 단어 세기 프로그램 공개 (6) | 2018.03.02 |