728x90
반응형
728x170
▶ Place.cs
namespace TestProject
{
/// <summary>
/// 장소
/// </summary>
public class Place
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 도시명
/// </summary>
private string cityName;
/// <summary>
/// 주
/// </summary>
private string state;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 도시명 - CityName
/// <summary>
/// 도시명
/// </summary>
public string CityName
{
get
{
return this.cityName;
}
set
{
this.cityName = value;
}
}
#endregion
#region 주 - State
/// <summary>
/// 주
/// </summary>
public string State
{
get
{
return this.state;
}
set
{
this.state = value;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - Place()
/// <summary>
/// 생성자
/// </summary>
public Place()
{
this.cityName = string.Empty;
this.state = string.Empty;
}
#endregion
#region 생성자 - Place(cityName, state)
/// <summary>
/// 생성자
/// </summary>
/// <param name="cityName">도시명</param>
/// <param name="state">주</param>
public Place(string cityName, string state)
{
this.cityName = cityName;
this.state = state;
}
#endregion
}
}
728x90
▶ PlaceCollection.cs
using System.Collections.ObjectModel;
namespace TestProject
{
/// <summary>
/// 장소 컬렉션
/// </summary>
public class PlaceCollection : ObservableCollection<Place>
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - PlaceCollection()
/// <summary>
/// 생성자
/// </summary>
public PlaceCollection()
{
Add(new Place("Seattle" , "WA"));
Add(new Place("Redmond" , "WA"));
Add(new Place("Bellevue" , "WA"));
Add(new Place("Kirkland" , "WA"));
Add(new Place("Portland" , "OR"));
Add(new Place("San Francisco", "CA"));
Add(new Place("Los Angeles" , "CA"));
Add(new Place("San Diego" , "CA"));
Add(new Place("San Jose" , "CA"));
Add(new Place("Santa Ana" , "CA"));
Add(new Place("Bellingham" , "WA"));
Add(new Place("Tacoma" , "WA"));
Add(new Place("Albany" , "OR"));
}
#endregion
}
}
300x250
▶ 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="ListBox 엘리먼트 : AlternationConverter 객체를 사용해 그룹 항목 배경색/전경색 설정하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Window.Resources>
<local:PlaceCollection x:Key="PlaceCollectionKey" />
<CollectionViewSource x:Key="CollectionViewSourceKey" Source="{StaticResource PlaceCollectionKey}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="State" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<AlternationConverter x:Key="GroupHeaderBackgroundAlternationConverterKey">
<SolidColorBrush>LightBlue</SolidColorBrush>
<SolidColorBrush>LightSteelBlue</SolidColorBrush>
</AlternationConverter>
<AlternationConverter x:Key="ItemBackgroundAlternationConverterKey">
<SolidColorBrush>Silver</SolidColorBrush>
<SolidColorBrush>LightGray</SolidColorBrush>
<SolidColorBrush>GhostWhite</SolidColorBrush>
</AlternationConverter>
</Window.Resources>
<Grid>
<ListBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="200"
Height="300"
Padding="5"
DisplayMemberPath="CityName"
AlternationCount="3"
ItemsSource="{Binding Source={StaticResource CollectionViewSourceKey}}">
<ListBox.GroupStyle>
<GroupStyle AlternationCount="2">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock
Padding="5"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}},
Path=(ItemsControl.AlternationIndex),
Converter={StaticResource GroupHeaderBackgroundAlternationConverterKey}}"
FontWeight="Bold"
Text="{Binding Path=Name}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="5" />
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(ItemsControl.AlternationIndex),
Converter={StaticResource ItemBackgroundAlternationConverterKey}}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] DrawingContext 클래스 : DrawRoundedRectangle 메소드를 사용해 라운드 사각형 그리기 (0) | 2020.09.11 |
---|---|
[C#/WPF] DrawingContext 클래스 : DrawRectangle 메소드를 사용해 사각형 그리기 (0) | 2020.09.11 |
[C#/WPF] Window 엘리먼트 : 테두리 없는 윈도우 그림자 효과 사용하기 (0) | 2020.09.11 |
[C#/WPF] DrawingGroup 엘리먼트 : Transform 속성 사용하기 (0) | 2020.09.11 |
[C#/WPF] DrawingImage 엘리먼트 : DrawingGroup 객체를 사용해 이미지 만들기 (0) | 2020.09.10 |
[C#/WPF] ListBox 엘리먼트 : AlternationConverter 객체를 사용해 리스트 박스 항목 배경색/전경색 설정하기 (0) | 2020.09.10 |
[C#/WPF] ListBox 엘리먼트 : AlternationIndex 첨부 속성을 사용해 리스트 박스 항목 배경색/전경색 설정하기 (0) | 2020.09.10 |
[C#/WPF] Button 엘리먼트 : ClickMode 속성 사용하기 (0) | 2020.09.10 |
[C#/WPF] AnimationClock 클래스 : 클럭 컨트롤러 사용하기 (0) | 2020.09.09 |
[C#/WPF] DiscreteDoubleKeyFrame 엘리먼트 : 초침을 끊어서 움직이기 (0) | 2020.09.09 |
댓글을 달아 주세요