[C#/WPF] DataGrid 엘리먼트 : 행 세부 정보 섹션 가로 스크롤 방지하기
■ DataGrid 엘리먼트에서 행 세부 정보 섹션의 가로 스크롤을 방지하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 |
<DataGrid AreRowDetailsFrozen="True" /> |
■ DataGrid 엘리먼트에서 행 세부 정보 섹션의 가로 스크롤을 방지하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 |
<DataGrid AreRowDetailsFrozen="True" /> |
■ DataGrid 엘리먼트의 RowDetailsVisibilityMode 속성을 사용하는 방법을 보여준다. ▶ Person.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 /// </summary> public class Person : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 이벤트 - PropertyChanged /// <summary> /// 속성 변경시 이벤트 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 회사명 /// <summary> private string companyName; /// <summary> /// 이름 /// <summary> private string firstName; /// <summary> /// 성 /// <summary> private string lastName; /// <summary> /// 이메일 주소 /// <summary> private string emailAddress; /// <summary> /// 전화 /// <summary> private string phone; /// <summary> /// 영업 담당 /// <summary> private string salesPerson; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 회사명 - CompanyName /// <summary> /// 회사명 /// </summary> public string CompanyName { get { return this.companyName; } set { if(this.companyName == value) { return; } this.companyName = value; FirePropertyChangedEvent(nameof(CompanyName)); } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get { return this.firstName; } set { if(this.firstName == value) { return; } this.firstName = value; FirePropertyChangedEvent(nameof(FirstName)); } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get { return this.lastName; } set { if(this.lastName == value) { return; } this.lastName = value; FirePropertyChangedEvent(nameof(LastName)); } } #endregion #region 이메일 주소 - EmailAddress /// <summary> /// 이메일 주소 /// </summary> public string EmailAddress { get { return this.emailAddress; } set { if(this.emailAddress == value) { return; } this.emailAddress = value; FirePropertyChangedEvent(nameof(EmailAddress)); } } #endregion #region 전화 - Phone /// <summary> /// 전화 /// </summary> public string Phone { get { return this.phone; } set { if(this.phone == value) { return; } this.phone = value; FirePropertyChangedEvent(nameof(Phone)); } } #endregion #region 영업 담당 - SalesPerson /// <summary> /// 영업 담당 /// </summary> public string SalesPerson { get { return this.salesPerson; } set { if(this.salesPerson == value) { return; } this.salesPerson = value; FirePropertyChangedEvent(nameof(SalesPerson)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="DataGrid 엘리먼트 : RowDetailsVisibilityMode 속성 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <DataGrid Name="dataGrid" AutoGenerateColumns="False" RowDetailsVisibilityMode="Visible" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="회사명" Binding="{Binding CompanyName}" /> <DataGridTextColumn Header="이름" Binding="{Binding FirstName}" /> <DataGridTextColumn Header="성" Binding="{Binding LastName}" /> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <Border BorderThickness="0" Background="BlanchedAlmond" Padding="10"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="메일 주소" /> <TextBlock Grid.Row="0" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding EmailAddress}" /> <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="전화" /> <TextBlock Grid.Row="1" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding Phone}" /> <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="영업 담당자" /> <TextBlock Grid.Row="2" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding SalesPerson}" /> </Grid> </Border> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using System.Collections.ObjectModel; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ObservableCollection<Person> collection = new ObservableCollection<Person>(); collection.Add ( new Person { CompanyName = "회사1", FirstName = "길동", LastName = "홍", EmailAddress = "id01@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사1", FirstName = "동구", LastName = "강", EmailAddress = "id02@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사2", FirstName = "희수", LastName = "김", EmailAddress = "id03@abc.com", Phone = "000-0000-0000", SalesPerson = "이영희" } ); this.dataGrid.ItemsSource = collection; } #endregion } } |
TestProject.zip
■ DataGrid 엘리먼트의 RowDetailsTemplate 속성을 사용하는 방법을 보여준다. ▶ Person.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 /// </summary> public class Person : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 이벤트 - PropertyChanged /// <summary> /// 속성 변경시 이벤트 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 회사명 /// <summary> private string companyName; /// <summary> /// 이름 /// <summary> private string firstName; /// <summary> /// 성 /// <summary> private string lastName; /// <summary> /// 이메일 주소 /// <summary> private string emailAddress; /// <summary> /// 전화 /// <summary> private string phone; /// <summary> /// 영업 담당 /// <summary> private string salesPerson; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 회사명 - CompanyName /// <summary> /// 회사명 /// </summary> public string CompanyName { get { return this.companyName; } set { if(this.companyName == value) { return; } this.companyName = value; FirePropertyChangedEvent(nameof(CompanyName)); } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get { return this.firstName; } set { if(this.firstName == value) { return; } this.firstName = value; FirePropertyChangedEvent(nameof(FirstName)); } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get { return this.lastName; } set { if(this.lastName == value) { return; } this.lastName = value; FirePropertyChangedEvent(nameof(LastName)); } } #endregion #region 이메일 주소 - EmailAddress /// <summary> /// 이메일 주소 /// </summary> public string EmailAddress { get { return this.emailAddress; } set { if(this.emailAddress == value) { return; } this.emailAddress = value; FirePropertyChangedEvent(nameof(EmailAddress)); } } #endregion #region 전화 - Phone /// <summary> /// 전화 /// </summary> public string Phone { get { return this.phone; } set { if(this.phone == value) { return; } this.phone = value; FirePropertyChangedEvent(nameof(Phone)); } } #endregion #region 영업 담당 - SalesPerson /// <summary> /// 영업 담당 /// </summary> public string SalesPerson { get { return this.salesPerson; } set { if(this.salesPerson == value) { return; } this.salesPerson = value; FirePropertyChangedEvent(nameof(SalesPerson)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="DataGrid 엘리먼트 : RowDetailsTemplate 속성 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <DataGrid Name="dataGrid" AutoGenerateColumns="False" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="회사명" Binding="{Binding CompanyName}" /> <DataGridTextColumn Header="이름" Binding="{Binding FirstName}" /> <DataGridTextColumn Header="성" Binding="{Binding LastName}" /> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <Border BorderThickness="0" Background="BlanchedAlmond" Padding="10"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="메일 주소" /> <TextBlock Grid.Row="0" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding EmailAddress}" /> <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="전화" /> <TextBlock Grid.Row="1" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding Phone}" /> <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="영업 담당자" /> <TextBlock Grid.Row="2" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding SalesPerson}" /> </Grid> </Border> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using System.Collections.ObjectModel; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ObservableCollection<Person> collection = new ObservableCollection<Person>(); collection.Add ( new Person { CompanyName = "회사1", FirstName = "길동", LastName = "홍", EmailAddress = "id01@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사1", FirstName = "동구", LastName = "강", EmailAddress = "id02@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사2", FirstName = "희수", LastName = "김", EmailAddress = "id03@abc.com", Phone = "000-0000-0000", SalesPerson = "이영희" } ); this.dataGrid.ItemsSource = collection; } #endregion } } |
TestProject.zip
■ Canvas 클래스에서 Thumb 엘리먼트를 이용해 캔버스 크기를 조정하는 방법을 보여준다. ▶ TestWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<Window x:Class="DS.Test.WPF.TestWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight"> <Canvas Name="canvas" Width="300" Height="300"> <Thumb Name="thumb" Canvas.Left="280" Canvas.Top="280" Width="20" Height="20" Background="Blue" DragStarted="thumb_DragStarted" DragDelta="thumb_DragDelta" DragCompleted="thumb_DragCompleted" /> </Canvas> </Window> |
▶ TestWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using System; using System.Windows; using System.Windows.Media; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace DS.Test.WPF { /// <summary> /// 테스트 윈도우 /// </summary> public partial class TestWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestWindow() /// <summary> /// 생성자 /// </summary> public TestWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Thumb 드래그 시작시 처리하기 - thumb_DragStarted(sender, e) /// <summary> /// Thumb 드래그 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void thumb_DragStarted(object sender, DragStartedEventArgs e) { this.thumb.Background = Brushes.Orange; } #endregion #region Thumb 드래그 변화시 처리하기 - thumb_DragDelta(sender, e) /// <summary> /// Thumb 드래그 변화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void thumb_DragDelta(object sender, DragDeltaEventArgs e) { double newCanvasWidth = this.canvas.Width + e.HorizontalChange; double newCanvasHeight = this.canvas.Height + e.VerticalChange; if((newCanvasWidth >= 0) && (newCanvasHeight >= 0)) { this.canvas.Width = newCanvasWidth; this.canvas.Height = newCanvasHeight; Canvas.SetLeft(this.thumb, Canvas.GetLeft(this.thumb) + e.HorizontalChange); Canvas.SetTop (this.thumb, Canvas.GetTop (this.thumb) + e.VerticalChange ); } } #endregion #region Thumb 드래그 완료시 처리하기 - thumb_DragCompleted(sender, e) /// <summary> /// Thumb 드래그 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void thumb_DragCompleted(object sender, DragCompletedEventArgs e) { this.thumb.Background = Brushes.Blue; } #endregion } } |
■ BulletDecorator 엘리먼트에서 Image 엘리먼트를 불릿으로 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<BulletDecorator HorizontalAlignment="Center" VerticalAlignment="Center" Background="Yellow"> <BulletDecorator.Bullet> <Image Height="50"> <Image.Source> <BitmapImage DecodePixelHeight="50" UriSource="c:\pinkcherries.jpg" /> </Image.Source> </Image> </BulletDecorator.Bullet> <TextBlock Width="100" TextWrapping="Wrap" HorizontalAlignment="Left" Foreground ="Purple"> A Simple BulletDecorator </TextBlock> </BulletDecorator> |
■ Label 엘리먼트의 Target 속성을 사용하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBox Name="textBox" HorizontalAlignment="Center" Width="200" Height="20" VerticalContentAlignment="Center" /> <Label HorizontalAlignment="Center" Margin="0 10 0 0" Width="200" Target="{Binding ElementName=textBox}"> <AccessText TextWrapping="WrapWithOverflow"> _Another long piece of text that requires text wrapping goes here. </AccessText> </Label> <TextBox HorizontalAlignment="Center" Margin="0 10 0 0" Width="200" Height="20" VerticalContentAlignment="Center" /> </StackPanel> </Window> |
TestProject.zip
■ CroppedBitmap 클래스를 사용해 비트맵 소스에서 비트맵을 잘라내는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#region CroppedBitmap 객체 구하기 - GetCroppedBitmap(bitmapSource, sourceRectangle) /// <summary> /// CroppedBitmap 객체 구하기 /// </summary> /// <param name="bitmapSource">비트맵 소스</param> /// <param name="sourceRectangle">소스 사각형</param> /// <returns>CroppedBitmap 객체</returns> public CroppedBitmap GetCroppedBitmap(BitmapSource bitmapSource, Int32Rect sourceRectangle) { CroppedBitmap croppedBitmap = new CroppedBitmap(); croppedBitmap.BeginInit(); croppedBitmap.Source = bitmapSource; croppedBitmap.SourceRect = sourceRectangle; croppedBitmap.EndInit(); return croppedBitmap; } #endregion |
■ FormatConvertedBitmap 클래스를 사용해 비트맵 소스의 픽셀 포맷을 변경하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#region FormatConvertedBitmap 객체 구하기 - GetFormatConvertedBitmap(bitmapSource, targetPixelFormat) /// <summary> /// FormatConvertedBitmap 객체 구하기 /// </summary> /// <param name="bitmapSource">비트맵 소스</param> /// <param name="targetPixelFormat">타겟 픽셀 포맷</param> /// <returns>FormatConvertedBitmap 객체</returns> public FormatConvertedBitmap GetFormatConvertedBitmap(BitmapSource bitmapSource, PixelFormat targetPixelFormat) { FormatConvertedBitmap formatConvertedBitmap = new FormatConvertedBitmap(); formatConvertedBitmap.BeginInit(); formatConvertedBitmap.Source = bitmapSource; formatConvertedBitmap.DestinationFormat = targetPixelFormat; formatConvertedBitmap.EndInit(); return formatConvertedBitmap; } #endregion |
■ FormatConvertedBitmap 엘리먼트의 DestinationFormat 속성을 사용해 비트맵의 픽셀 포맷을 변경하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 |
<FormatConvertedBitmap DestinationFormat="Gray32Float"> <FormatConvertedBitmap.Source> <BitmapImage DecodePixelWidth="200" UriSource="c:\pinkcherries.jpg" /> </FormatConvertedBitmap.Source> </FormatConvertedBitmap> |
■ BitmapImage 클래스를 사용해 크기 조정 비트맵 이미지를 구하는 방법을 보여준다. ▶ BitmapImage 클래스 : 크기 조정 비트맵 이미지 구하기 예제 (C#)
■ BitmapImage 엘리먼트의 DecodePixelWidth/DecodePixelHeight 속성을 사용하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 |
<Image Width="200"> <Image.Source> <BitmapImage DecodePixelWidth="200" UriSource="C:\pinkcherries.jpg" /> </Image.Source> </Image> |
※ DecodePixelWidth 특성만 설정하면 DecodePixelHeight는 이미지 원본 크기에 비례해서
■ Image 엘리먼트의 Width 속성을 사용하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 |
<Image Width="200" Source="C:\pinkcherries.jpg" /> |
※ Width 특성만 설정하면 Height는 이미지 원본 크기에 비례해서
■ Storyboard 클래스를 사용해 코드로 구현하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="Storyboard 클래스 : 코드 구현하기" FontFamily="나눔고딕코딩" FontSize="16"> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Animation; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { // Page 객체에 대해 NameScope 객체를 설정합니다. NameScope.SetNameScope(this, new NameScope()); #region StackPanel 객체를 생성한다. StackPanel stackPanel = new StackPanel(); stackPanel.HorizontalAlignment = HorizontalAlignment.Center; Content = stackPanel; #endregion #region Border 객체를 생성한다. Border border = new Border(); border.Margin = new Thickness(0, 60, 0, 20); border.BorderBrush = Brushes.Black; border.BorderThickness = new Thickness(1); border.Background = Brushes.Gray; border.Padding = new Thickness(20); stackPanel.Children.Add(border); // Storyboard.TargetName 첨부 속성 설정을 위해 Border 객체의 명칭을 등록한다. RegisterName("border", border); #endregion #region ThicknessAnimation 객체를 생성한다. ThicknessAnimation thicknessAnimation = new ThicknessAnimation(); thicknessAnimation.RepeatBehavior = RepeatBehavior.Forever; thicknessAnimation.AutoReverse = true; thicknessAnimation.Duration = TimeSpan.FromSeconds(1); thicknessAnimation.From = new Thickness(1, 1, 1, 1); thicknessAnimation.To = new Thickness(56, 28, 56, 28); Storyboard.SetTargetName(thicknessAnimation, "border"); Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath(Border.BorderThicknessProperty)); #endregion #region Storyboard 객체를 생성한다. Storyboard storyboard = new Storyboard(); storyboard.Children.Add(thicknessAnimation); #endregion border.Loaded += delegate(object sender, RoutedEventArgs e) { storyboard.Begin(this); }; } #endregion } } |
TestProject.zip
■ ControlTemplate 엘리먼트에서 RelativeSource 태그 확장의 TemplatedParent 속성을 사용하는 방법을 보여준다. ▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="ControlTemplate 엘리먼트 : RelativeSource 태그 확장에서 TemplatedParent 모드 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" BorderThickness="2" BorderBrush="Black" CornerRadius="20"> <Border.Background> <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5"> <GradientStop Offset="0.0" Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" /> <GradientStop Offset="0.9" Color="White" /> </LinearGradientBrush> </Border.Background> <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="border" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0 0.5" EndPoint="1 0.5"> <GradientStop Offset="0.0" Color="{Binding Background.Color, RelativeSource={RelativeSource TemplatedParent}}" /> <GradientStop Offset="0.9" Color="DarkSlateGray" /> </LinearGradientBrush> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Width="300" Height="300" Background="RoyalBlue" FontWeight="Bold" FontSize="18"> <TextBlock FontSize="48"> 테스트 </TextBlock> </Button> </Grid> </Window> |
TestProject.zip
■ GridControl 엘리먼트에서 ICollectionView 데이터 소스를 바인딩하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System.Collections.Generic; using System.Windows.Data; using System.ComponentModel; /// <summary> /// 뷰 모델 /// </summary> public class ViewModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컬렉션 뷰 /// </summary> private ICollectionView collectionView; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 컬렉션 뷰 - CollectionView /// <summary> /// 컬렉션 뷰 /// </summary> public ICollectionView CollectionView { get { if(this.collectionView == null) { List<SampleData> list = new List<SampleData>(); for(int i = 0; i < 100; i++) { list.Add(new SampleData() { Number1 = i, Number2 = i * 10, Text1 = "row " + i, Text2 = "ROW " + i }); } this.collectionView = new ListCollectionView(list); } return this.collectionView; } } #endregion } |
▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<Grid xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:local="clr-namespace:HowToBindTheDXGridToICollectionView"> <Grid.DataContext> <local:ViewModel /> </Grid.DataContext> <dxg:GridControl x:Name="gridControl" ItemsSource="{Binding Path=CollectionView}" AutoGenerateColumns="AddNew"> <dxg:GridControl.View> <dxg:TableView IsSynchronizedWithCurrentItem="True" ShowGroupPanel="False" AllowGrouping="False" AutoWidth="True" AllowEditing="False" ShowAutoFilterRow="True" /> </dxg:GridControl.View> </dxg:GridControl> </Grid> |
■ WPF 버전 번호가 저장된 레지스트리 키를 보여준다. ▶ 레지스트리 키
1 2 3 |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation |
■ BrowserInteropHelper 클래스의 HostScript 속성을 사용해 XBAP 호스트 웹 페이지와 통신하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 |
using System.Windows.Interop; dynamic hostScript = BrowserInteropHelper.HostScript; ... hostScript.Close(); // 웹 브라우저를 닫는다. |
※ BrowserInteropHelper.HostScript 속성은 HTML
■ Application 클래스에서 격리된 저장소에 애플리케이션 속성을 저장하고 로드하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using System.IO; using System.IO.IsolatedStorage; using System.Windows; namespace DS.Test.WPF { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 경로 /// </summary> private string filePath = "Application.txt"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStartup(e) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnStartup(StartupEventArgs e) { IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain(); try { using(IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(this.filePath, FileMode.Open, isolatedStorageFile)) { using(StreamReader streamReader = new StreamReader(isolatedStorageFileStream)) { while(!streamReader.EndOfStream) { string[] keyValueArray = streamReader.ReadLine().Split(new char[] {','}); Properties[keyValueArray[0]] = keyValueArray[1]; } } } } catch(FileNotFoundException fileNotFoundException) { MessageBox.Show(fileNotFoundException.Message); } } #endregion #region 종료시 처리하기 - OnExit(e) /// <summary> /// 종료시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnExit(ExitEventArgs e) { IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain(); using(IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(this.filePath, FileMode.Create, isolatedStorageFile)) { using(StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream)) { foreach(string key in this.Properties.Keys) { streamWriter.WriteLine("{0},{1}", key, Properties[key]); } } } } #endregion } } |
■ Application 클래스의 FindResource 메소드를 사용해 리소스를 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 |
using System.Windows; using System.Windows.Media; LinearGradientBrush brush = Application.Current.FindResource("LinearGradientBrushKey") as LinearGradientBrush; |
■ ResourceDictionary 엘리먼트를 사용하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 |
<ResourceDictionary Source="SampleResourceDictionary.xaml" /> |
■ Application 클래스의 Shutdown 메소드를 사용해 애플리케이션을 중지하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 |
using System.Windows; Application application = Application.Current; application.Shutdown(); |
■ Enum 클래스를 사용해 문자열로 멤버 값을 구하는 방법을 보여준다. ▶ Enum 클래스 : 문자열로 멤버 값 구하기 예제 (C#)
1 2 3 |
MessageBoxOptions messageBoxOptions = GetEnumerationValue<MessageBoxOptions>("YesNo"); |
▶
■ Window 클래스를 사용해 투명 윈도우를 만드는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStyle="None" AllowsTransparency="True" Background="Transparent" SizeToContent="WidthAndHeight"> <Button Width="100" Height="30"> 닫기 </Button> </Window> |
■ SaveFileDialog 클래스를 사용해 이미지 파일 저장 대화 상자를 표시하는 방법을 보여준다. ▶ SaveFileDialog 클래스 : 이미지 파일 저장 대화 상자 표시하기
■ SaveFileDialog 클래스를 사용해 파일 저장 대화 상자를 표시하는 방법을 보여준다. ▶ SaveFileDialog 클래스 : 파일 저장 대화 상자 표시하기 예제 (C#)