하고재비
[WPF] User Control Example for WPF 본문
UserControl for WPF
특정 UI구현을 반복하거나, 이전의 프로그램 UI를 같이 사용하고 싶을때 사용
1. 사용자 정의 컨트롤 라이브러리 작성
2. UserControl code
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 | <!-- XAML --> <UserControl x:Class="WpfControlLibrary1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfControlLibrary1" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Label x:Name="label" Content="UserControl_ Example" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="280"/> </Grid> </UserControl> <!-- .cs Code --> namespace WpfControlLibrary1 { /// <summary> /// UserControl1.xaml에 대한 상호 작용 논리 /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); label.Content = "DeadDE"; } } } | cs |
3. 사용할 WPF Project 생성
4. Project Directory의 Debug폴더 내부 'WpfControlLibrary1.dll' 참조 추가
5. Usercontrol 사용할 프로젝트 coding
5-1.사용할 Usercontrol 생성 (Namespace.class)
1 | WpfControlLibrary1.UserControl1 myControler; | cs |
Name Space = WpfControlLibrary1
class = UserControl1
5-2. Code
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 | <!-- XAML --> <Window x:Class="WpfApplication5_UsingUsercontrol.MainWindow" 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:WpfApplication5_UsingUsercontrol" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid Name="contentinit" Margin="10"> <GroupBox Name="Group_Box_1" Header="grb1" Margin="10,34,10,171"/> <GroupBox x:Name="Group_Box_2" Header="grb2" Margin="10,133,10,66" Height="100"/> </Grid> </Window> <!-- .cs --> namespace WpfApplication5_UsingUsercontrol { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainWindow : Window { WpfControlLibrary1.UserControl1 myControler; WpfControlLibrary1.UserControl1 myControler2; public MainWindow() { InitializeComponent(); getUserControl(); } private void getUserControl() { myControler = new WpfControlLibrary1.UserControl1(); Group_Box_1.Content = myControler; myControler2 = new WpfControlLibrary1.UserControl1(); Group_Box_2.Content = myControler2; } } } | cs |
6. 결과
'C# Windows Forms WPF' 카테고리의 다른 글
[C#] 프로그램 방식의 click event 발생 (0) | 2019.09.03 |
---|---|
[C#] 지수 변환 (0) | 2019.06.14 |
[C#] 콘솔창 유지 (0) | 2019.06.02 |
[WPF] Datagrid and MDB (0) | 2018.12.09 |
[C#] C# Log4Net log남기기 (0) | 2018.12.09 |
Comments