WPF 스타일 BasedOn 연결 질문

안녕하세요. :smile:

WPF 스타일(BasedOn)에 관한 질문 드립니다.

Based 스타일을 상속받기 위해서는 BasedOn을 지정해야 한다는 것을 알고 있습니다.
하지만 x:Key를 통해 접근하지 않고도 상속받을 수 있는 방법이 있을까요?

먼저 구조를 설명드리겠습니다.
아래는 제가 구현한 클래스 구조입니다.

// AssemblyA.dll
public class Preview : ContentControl
{
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Preview), 
            new FrameworkPropertyMetadata(typeof(Preview)));
}
// AssemblyB.dll
public class SwitchSkin : Preview
{
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchSkin), 
            new FrameworkPropertyMetadata(typeof(SwitchSkin)));
}

// SwitchSkin > Preview > ContentControl

두 객체는 서로 다른 어셈블리에 위치하고 있으므로 DefaultStyleKeyProperty를 통해 등록된 스타일(Generic.xaml)도 물리적으로 떨어져있습니다.


그래서 아래와 같이 해결했는데요.

<!-- AssemblyA.Themes.Generic.xaml -->
<Style TargetType="{x:Type Preview}" x:Key="PREVIEW">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Preview">
                <StackPanel>
                    <TextBlock Text="Preview"/>
                    <ContentPresenter/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- Preview 연결 -->
<Style TargetType="{x:Type Preview}" BasedOn="{StaticResource PREVIEW"/>
<!-- AssemblyB.Themes.Generic.xaml -->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/AssemblyA;component/...xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style TargetType="{x:Type SkinSwitch}" BasedOn="{StaticResource PREVIEW}">
    <Setter Property="Content">
        <Setter.Value>
            <TextBlock Text="Content"/>
        </Setter.Value>
    </Setter>
</Style>

혹시 BasedOn을 선언할 떄 x:Key를 직접 선언하지 않고도 Preview 클래스의 기본 스타일을 상속받을 수 있는 방법이 있을까요?

가령… (희망회로)

BasedOn="{x:DefaultStyleType flows:Preview}"

읽어주셔서 감사합니다. :smile:

1개의 좋아요
<Style ... BasedOn="{StaticResource {x:Type flows:Preview}}"/>

추측이긴 하지만 이렇게는 안되나요?

3개의 좋아요

@level120 헉 너무 잘됩니다… 감사합니다. :smile:

1개의 좋아요