WPF 및 C# 공부중....
WPF를 공부하기 위해 간단한 프로그램을 작성해 봤다.
Grid 로 데이타를 출력하는 프로그램이다.
Grid 의 칼럼들이 기본으로 왼쪽 정렬되어있는데, 이게 영 보기가 좋지 않다.
그래서 오른쪽으로 정렬을 하려는데 잘 안되다.
대부분의 코드들이 XAML (자멜)을 사용하고 있으며 복잡하다.
그래서 XAML 을 사용하지 않고, 오른쪽 정렬을 하는 프로그램을 작성해봤다.
바로 아래와 같이 모양이 좋지 않다.
그래서 삽질끝에 간단하게 수정해 봤다.
리스트 뷰의 칼럼을 오른쪽으로 정렬하려면 아래와 같은 구조의 XAML 이 필요하다.
즉, 오브젝트의 계층관계를 알 수 있다.
먼저, ListView 를 사용할 클래스를 작성한다. (그냥 리스트뷰를 써도 무방하다.)
실제 텍스트 블락을 추가해 보자.
보통의 WPF 책에서는 (찰스 페졸드의 WPF 책에서도 위의 코드가 없이 아래의 코드만 나와 있다. 그래서, 이 고생을 했다. 그의 책의 예제들도 오른쪽 정렬이 안된다. 예, 16장의 트리와 리스트뷰 예제들...
그리고, 어떤 블로그에는 텍스트블락의 크기를 늘려서 편법으로 해결하는 것을 본적이 있는데,
셀이 늘어나도 텍스트블락은 늘어나지 않기 때문에 문제가 있다.
아래 코드는 일반적이므로, 위의 코드만 작성하면, 간단히 해결되는 문제였다.)
위 코드의 세줄만 추가해주면 원하는 결과가 바로 나온다.
WPF 관련 링크 수집중...
MSDN WPF Forum
WPF WIKI
Getting Started in WPF
Learn WPF
GPU Based Effects for WPF
MSDN WPF Blogger 모음
WPF and C# Tip
WPF Studio
XAML Learning Guide
WPF Samples
E-Book
http://codebetter.com/files/folders/codebetter_downloads/entry179694.aspx
http://www.charlespetzold.com/dotnet/index.html
WPF를 공부하기 위해 간단한 프로그램을 작성해 봤다.
Grid 로 데이타를 출력하는 프로그램이다.
Grid 의 칼럼들이 기본으로 왼쪽 정렬되어있는데, 이게 영 보기가 좋지 않다.
그래서 오른쪽으로 정렬을 하려는데 잘 안되다.
대부분의 코드들이 XAML (자멜)을 사용하고 있으며 복잡하다.
그래서 XAML 을 사용하지 않고, 오른쪽 정렬을 하는 프로그램을 작성해봤다.
바로 아래와 같이 모양이 좋지 않다.
그래서 삽질끝에 간단하게 수정해 봤다.
리스트 뷰의 칼럼을 오른쪽으로 정렬하려면 아래와 같은 구조의 XAML 이 필요하다.
즉, 오브젝트의 계층관계를 알 수 있다.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Column">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="<바인딩할 필드이름>" TextAlignment="Right"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Column">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="<바인딩할 필드이름>" TextAlignment="Right"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
먼저, ListView 를 사용할 클래스를 작성한다. (그냥 리스트뷰를 써도 무방하다.)
public class MYListView : ListView
{
GridView grdvue;
public MYListView()
{
/// 위의 계층처럼, 리스트뷰의 뷰를 그리드 뷰로 설정한다.
grdvue = new GridView();
this.View = grdvue;
/// 그리드 뷰의 아이템 항목을 오른쪽으로 정렬하기 위해, 스타일을 수정함.
/// 아이템의 오브젝트를 늘림. (즉, TextBlock 을 칼럼에 맞게 미리 늘려주는 것이다.)
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
/// 아래와 같이 ItemContainterStyle 을 바로 적용해 준다.
this.ItemContainerStyle = style;
...
{
GridView grdvue;
public MYListView()
{
/// 위의 계층처럼, 리스트뷰의 뷰를 그리드 뷰로 설정한다.
grdvue = new GridView();
this.View = grdvue;
/// 그리드 뷰의 아이템 항목을 오른쪽으로 정렬하기 위해, 스타일을 수정함.
/// 아이템의 오브젝트를 늘림. (즉, TextBlock 을 칼럼에 맞게 미리 늘려주는 것이다.)
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
/// 아래와 같이 ItemContainterStyle 을 바로 적용해 준다.
this.ItemContainerStyle = style;
...
실제 텍스트 블락을 추가해 보자.
보통의 WPF 책에서는 (찰스 페졸드의 WPF 책에서도 위의 코드가 없이 아래의 코드만 나와 있다. 그래서, 이 고생을 했다. 그의 책의 예제들도 오른쪽 정렬이 안된다. 예, 16장의 트리와 리스트뷰 예제들...
그리고, 어떤 블로그에는 텍스트블락의 크기를 늘려서 편법으로 해결하는 것을 본적이 있는데,
셀이 늘어나도 텍스트블락은 늘어나지 않기 때문에 문제가 있다.
아래 코드는 일반적이므로, 위의 코드만 작성하면, 간단히 해결되는 문제였다.)
위 코드의 세줄만 추가해주면 원하는 결과가 바로 나온다.
// 칼럼의 타이틀(헤더)와 바인딩할 필드를 매개변수로 한다.
public void MyColumnAdd(string tag, string binding)
{
GridViewColumn col = new GridViewColumn();
FrameworkElementFactory contentFact = new FrameworkElementFactory();
col.Header = tag; /// 타이틀
/// 출력할 필드를 뿌려줄 텍스트 블락을 칼럼의 Cell 에 붙인다.
contentFact.Type = typeof(TextBlock);
/// 스타일 수정
Style style = new Style(typeof(TextBlock));
/// 오른쪽 정렬을 한다.
style.Setters.Add(new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right));
contentFact.SetValue(TextBlock.StyleProperty, style);
contentFact.SetBinding(TextBlock.TextProperty, new Binding(binding));
DataTemplate dt = new DataTemplate();
// 텍스트 블락 길이 테스트
dt.VisualTree = contentFact;
col.CellTemplate = dt;
/// 생성한 칼럼을 그리드뷰에 추가해주면 된다.
grdvue.Columns.Add(col);
}
public void MyColumnAdd(string tag, string binding)
{
GridViewColumn col = new GridViewColumn();
FrameworkElementFactory contentFact = new FrameworkElementFactory();
col.Header = tag; /// 타이틀
/// 출력할 필드를 뿌려줄 텍스트 블락을 칼럼의 Cell 에 붙인다.
contentFact.Type = typeof(TextBlock);
/// 스타일 수정
Style style = new Style(typeof(TextBlock));
/// 오른쪽 정렬을 한다.
style.Setters.Add(new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right));
contentFact.SetValue(TextBlock.StyleProperty, style);
contentFact.SetBinding(TextBlock.TextProperty, new Binding(binding));
DataTemplate dt = new DataTemplate();
// 텍스트 블락 길이 테스트
dt.VisualTree = contentFact;
col.CellTemplate = dt;
/// 생성한 칼럼을 그리드뷰에 추가해주면 된다.
grdvue.Columns.Add(col);
}
WPF 관련 링크 수집중...
MSDN WPF Forum
WPF WIKI
Getting Started in WPF
Learn WPF
GPU Based Effects for WPF
MSDN WPF Blogger 모음
WPF and C# Tip
WPF Studio
XAML Learning Guide
WPF Samples
E-Book
http://codebetter.com/files/folders/codebetter_downloads/entry179694.aspx
http://www.charlespetzold.com/dotnet/index.html
'컴퓨터' 카테고리의 다른 글
| Google Earth 는 어떻게 동작하는가.... (0) | 2008/07/23 |
|---|---|
| rikaichan 과 pera-pera kun 플러그인 (0) | 2008/07/15 |
| WPF 에서 Grid 의 칼럼 오른쪽 정렬하기... (0) | 2008/06/27 |
| 오늘은 불여우 3 내려받기 날입니다. (1) | 2008/06/18 |
| 불여우(Firefox)용 한글 문법검사기 플러그인 (0) | 2008/06/12 |
| nLite 를 이용해서 나만의 설치 CD 만들기 (0) | 2008/05/30 |
