반응형
WPF 사용자 정의 컨트롤에서 가져온 리소스와 로컬 리소스를 결합하는 방법
공유 및 개별 리소스가 모두 필요한 여러 WPF 사용자 컨트롤을 작성하고 있습니다.
별도의 리소스 파일에서 리소스를로드하는 구문을 알아 냈습니다.
<UserControl.Resources>
<ResourceDictionary Source="ViewResources.xaml" />
</UserControl.Resources>
그러나 이렇게하면 다음과 같이 로컬로 리소스를 추가 할 수도 없습니다.
<UserControl.Resources>
<ResourceDictionary Source="ViewResources.xaml" />
<!-- Doesn't work: -->
<ControlTemplate x:Key="validationTemplate">
...
</ControlTemplate>
<style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
...
</style>
...
</UserControl.Resources>
ResourceDictionary.MergedDictionaries를 살펴 보았지만 로컬에서 추가 리소스를 정의하지 않고 둘 이상의 외부 사전 만 병합 할 수 있습니다.
나는 사소한 것을 놓치고 있는가?
언급해야합니다. WinForms 프로젝트에서 사용자 컨트롤을 호스팅하고 있으므로 공유 리소스를 App.xaml에 넣는 것은 실제로 옵션이 아닙니다.
나는 그것을 알아. 솔루션에는 MergedDictionaries가 포함되지만 다음과 같이 세부 사항이 옳 아야합니다.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ViewResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- This works: -->
<ControlTemplate x:Key="validationTemplate">
...
</ControlTemplate>
<style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
...
</style>
...
</ResourceDictionary>
</UserControl.Resources>
즉, 로컬 리소스는 ResourceDictionary 태그 내에 중첩되어야합니다 . 따라서 여기 의 예 는 올바르지 않습니다.
MergedDictionaries 섹션에서 로컬 리소스를 정의 할 수 있습니다.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- import resources from external files -->
<ResourceDictionary Source="ViewResources.xaml" />
<ResourceDictionary>
<!-- put local resources here -->
<Style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
...
</Style>
...
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
MergedDictionaries를 사용하십시오 .
파일 1
<ResourceDictionary
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml " >
<Style TargetType="{x:Type TextBlock}" x:Key="TextStyle">
<Setter Property="FontFamily" Value="Lucida Sans" />
<Setter Property="FontSize" Value="22" />
<Setter Property="Foreground" Value="#58290A" />
</Style>
</ResourceDictionary>
파일 2
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TextStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
반응형
'development' 카테고리의 다른 글
문자열이 다른 문자열로 시작하는지 또는 끝나는 지 테스트 (0) | 2020.09.24 |
---|---|
DTD에서 PCDATA와 CDATA의 차이점 (0) | 2020.09.24 |
Wireshark vs Firebug vs Fiddler-장단점? (0) | 2020.09.24 |
파일을 사전으로 변환하는 방법은 무엇입니까? (0) | 2020.09.24 |
여러 번 붙여 넣기 (0) | 2020.09.24 |