본문 바로가기

Programming/C#

C#(WPF) - Datagrid 멀티라인 추가하기

반응형

아래와 같이 xaml에서 직접 작성할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<Window
	x:Class="ebar.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="ebar"
	Width="1175"
	Height="704">
	<Grid>
		<DataGrid
			FontFamily="Arial"
			x:Name="worktable"
			Height="133"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Stretch"
			VerticalAlignment="Top"
			Margin="30,128,34,0">
			<DataGrid.Columns>
				<DataGridTemplateColumn>
					<DataGridTemplateColumn.Header>
						<Grid Width="500">
							<Grid.RowDefinitions>
								<RowDefinition/>
								<RowDefinition/>
							</Grid.RowDefinitions>
							<Grid.ColumnDefinitions>
								<ColumnDefinition/>
								<ColumnDefinition/>
								<ColumnDefinition/>
								<ColumnDefinition/>
							</Grid.ColumnDefinitions>
							<TextBlock Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Main Header1" Grid.Column="0" />
							<TextBlock Grid.Row="1" Grid.Column="0" Text="Id1" />
							<TextBlock Grid.Row="1" Grid.Column="1" Text="Name1" />
							<TextBlock Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Main Header2" Grid.Column="2" />
							<TextBlock Grid.Row="1" Grid.Column="2" Text="Id1" />
							<TextBlock Grid.Row="1" Grid.Column="3" Text="Name2" />
						</Grid>
					</DataGridTemplateColumn.Header>					
				</DataGridTemplateColumn>
			</DataGrid.Columns>
		</DataGrid>
		<ComboBox
			Height="20"
			RenderTransformOrigin="0.5,0.6"
			Width="120"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="158,75.5,0,0"></ComboBox>
		<Label
			x:Name="Project_Label"
			Content="Project No"
			Width="133"
			Height="29"
			Grid.Column="0"
			Grid.Row="0"
			VerticalAlignment="Top"
			HorizontalAlignment="Left"
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Margin="35,70,0,0"
			RenderTransformOrigin="0.5,0.5" />
		<CheckBox
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="511,78.5,0,0" />
		<Label
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Content="진행중 Only"
			Width="84"
			Height="26"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="427,74,0,0" />
		<ComboBox
			Width="120"
			Height="20"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="683,75.5,0,0" />
		<Label
			Content="Unit(호기)"
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Width="123"
			Height="23"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="560,74.5,0,0" />
		<Label
			Content="작업Category"
			Width="100"
			Height="28"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="886,71.5,0,0" />
		<ComboBox
			Width="120"
			Height="20"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="994,76,0,0" />
	</Grid>
</Window>

아래와 같이 C#코드로 작성할 수도 있습니다.

/*
 * Created by SharpDevelop.
 * User: wijkim
 * Date: 2023-02-13
 * Time: 오후 2:56
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace ebar
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
			
			DataGridTextColumn textColumn = new DataGridTextColumn();
			textColumn.Header = "Column 1";
			textColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
			textColumn.HeaderTemplate = new DataTemplate();
			
			FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
			stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
			
			FrameworkElementFactory firstLine = new FrameworkElementFactory(typeof(TextBlock));
			firstLine.SetValue(TextBlock.TextProperty, "First line");
			firstLine.SetValue(TextBlock.FontSizeProperty, 16d);
			firstLine.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
			
			FrameworkElementFactory secondLine = new FrameworkElementFactory(typeof(TextBlock));
			secondLine.SetValue(TextBlock.TextProperty, "Second line");
			secondLine.SetValue(TextBlock.FontSizeProperty, 12d);
			secondLine.SetValue(TextBlock.FontStyleProperty, FontStyles.Italic);
			
			stackPanel.AppendChild(firstLine);
			stackPanel.AppendChild(secondLine);
			
			textColumn.HeaderTemplate.VisualTree = stackPanel;
			
			worktable.Columns.Add(textColumn);		
		}
	}
}

 

이 경우 XAML코드에서 필요없는 Column정의부분은 지워도 무관합니다.

<?xml version="1.0" encoding="utf-8"?>
<Window
	x:Class="ebar.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="ebar"
	Width="1175"
	Height="704">
	<Grid>
		<DataGrid
			FontFamily="Arial"
			x:Name="worktable"
			Height="133"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Stretch"
			VerticalAlignment="Top"
			Margin="30,128,34,0">
			<!--<DataGrid.Columns>
				<DataGridTemplateColumn>
					<DataGridTemplateColumn.Header>
						<Grid Width="500">
							<Grid.RowDefinitions>
								<RowDefinition/>
								<RowDefinition/>
							</Grid.RowDefinitions>
							<Grid.ColumnDefinitions>
								<ColumnDefinition/>
								<ColumnDefinition/>
								<ColumnDefinition/>
								<ColumnDefinition/>
							</Grid.ColumnDefinitions>
							<TextBlock Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Main Header1" Grid.Column="0" />
							<TextBlock Grid.Row="1" Grid.Column="0" Text="Id1" />
							<TextBlock Grid.Row="1" Grid.Column="1" Text="Name1" />
							<TextBlock Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Main Header2" Grid.Column="2" />
							<TextBlock Grid.Row="1" Grid.Column="2" Text="Id1" />
							<TextBlock Grid.Row="1" Grid.Column="3" Text="Name2" />
						</Grid>
					</DataGridTemplateColumn.Header>					
				</DataGridTemplateColumn>
			</DataGrid.Columns>-->
		</DataGrid>
		<ComboBox
			Height="20"
			RenderTransformOrigin="0.5,0.6"
			Width="120"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="158,75.5,0,0"></ComboBox>
		<Label
			x:Name="Project_Label"
			Content="Project No"
			Width="133"
			Height="29"
			Grid.Column="0"
			Grid.Row="0"
			VerticalAlignment="Top"
			HorizontalAlignment="Left"
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Margin="35,70,0,0"
			RenderTransformOrigin="0.5,0.5" />
		<CheckBox
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="511,78.5,0,0" />
		<Label
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Content="진행중 Only"
			Width="84"
			Height="26"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="427,74,0,0" />
		<ComboBox
			Width="120"
			Height="20"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="683,75.5,0,0" />
		<Label
			Content="Unit(호기)"
			HorizontalContentAlignment="Center"
			VerticalContentAlignment="Center"
			Width="123"
			Height="23"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="560,74.5,0,0" />
		<Label
			Content="작업Category"
			Width="100"
			Height="28"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="886,71.5,0,0" />
		<ComboBox
			Width="120"
			Height="20"
			Grid.Column="0"
			Grid.Row="0"
			HorizontalAlignment="Left"
			VerticalAlignment="Top"
			Margin="994,76,0,0" />
	</Grid>
</Window>

 

반응형