WPF XAML로 다른 클래스 라이브러리에 있는 xaml 참조하는 방법
Code/C#2019. 3. 12. 08:27
예를 들어 프로젝트 구조가
위 사진처럼 CLM_RibbonView에서 다른 클래스 라이브러리에 있는 CLM_RibbonStyle.xaml을 참조 하고 싶을때 사용하는 방법입니다.
- CLM_RibbonView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <UserControl x:Class="CLM.Views.CLM_RibbonView" 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:CLM.Views" xmlns:p="clr-namespace:CLM.Properties" xmlns:customstyle="clr-namespace:NH80.Config;assembly=NH80.Config" mc:Ignorable="d" VerticalAlignment="Top" Height="110" > <UserControl.Resources> <!--<ResourceDictionary Source="../Style/CLM_RibbonStyle.xaml"/>--> <!--<ResourceDictionary Source="../../../NH80.Config/Style/CLM_RibbonStyle.xaml"/>--> <!--<ResourceDictionary Source="/NH80.Config;../Style/CLM_RibbonStyle.xaml"/>--> <!--<ResourceDictionary Source="../../NH80.Config\Style\CLM_RibbonStyle.xaml"></ResourceDictionary>--> <ResourceDictionary Source="pack://application:,,,/NH80.Config;component/Style/CLM_RibbonStyle.xaml"/> </UserControl.Resources> | cs |
먼저 UserCibtrol로 만들어진 해당 뷰는 내부적으로 상단에 ResourceDictionary를 받아 올 수 있는 코드를 추가해 주었습니다.
주석은 저의 여러가지 시도들이구요.
결과적으로는 pack을 이용한 uri 참조가 성공적으로 빌드 되고 프로그램에서 작동했습니다.
-CLM_RibbonViewStyle
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 | <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <!--Private--> <!--Public--> <!--Ribbon 메뉴버튼 스타일--> <Style x:Key="roundbutton" TargetType="{x:Type Button}"> <Setter Property="Width" Value="60" /> <Setter Property="Margin" Value="0" /> <Setter Property="Height" Value="68" /> <!--<Setter Property="Background" Value="Transparent"/> --><!--배경색--> <Setter Property="Background" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border> <Grid > <Rectangle Name="m_Rectangle" Fill="#FFDDDDDD" Stroke="{x:Null}" RadiusX="5" RadiusY="5" /> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> | cs |
Ribbonview에서 참조해야 하는 xaml 파일은 style이 지정되어 있는 파일로써 ResourceDictionary로 만들어져 있습니다.
=============
설명
해당 클래스 라이브러리를 기본적으로 ../.. 등의 키워드로 참조하지 못하는 이유는, 어셈블리를 찾을 수 없기 때문입니다. 그렇기 때문에 Path 값을 상대적으로 지정해주면서 어셈블리를 명을 주어야 올바르게 프로그램에서 작동 할 수 있습니다.
그 과정에서 pack이라는 url 찾는 키워드를 이용하여 어셈블리를 넘겨주면서 파일을 불러왔습니다.
'Code > C#' 카테고리의 다른 글
WPF Combobox 선택 이벤트, old 값이 나올때 (0) | 2019.03.19 |
---|---|
WPF C#으로 UserControll을 바로 다이얼로그 화면으로 출력 하고 싶을때 (0) | 2019.03.12 |
WPF TextBlock에 다중 바인딩으로 글자 표현하는 방법 (0) | 2019.03.11 |
Mvvm이란 무엇인가 (0) | 2019.02.28 |
[WPF] DelegateCommand 사용법 (0) | 2019.02.20 |