How to make a style based on a resource that is different from StaticResource

590 Views Asked by At

I have a window which has a ResourceDictionary in Resources. I made a CheckBox style which is based on the one that is defined in the ResourceDictionary. What is more, I'd like to make a RadioButton based on a ToggleButton style, which is NOT based on the one that is defined in the ResourceDictionary. I used ControlTemplate as shown in the following code, but in this case, TemplateBinding is necessary for all the properties involved. Is there a better way to realize my purpose without using ControlTemplate?

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/theme;component/BasicTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<Grid>

    <Grid.Resources>
        <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
            <!-- Several style setters -->
        </Style>
        <Style x:Key="Edit"/>
        <ControlTemplate x:Key="ToggleTemplate" TargetType="{x:Type ToggleButton}">
            <ToggleButton Style="{StaticResource Edit}"
                      Content="{TemplateBinding Content}"
                      IsChecked="{TemplateBinding IsChecked}"/>
        </ControlTemplate>
    </Grid.Resources>

    <CheckBox/>
    <RadioButton Template="{DynamicResource ToggleTemplate}"/>

</Grid>

The following code didn't work properly.

<Grid>

    <Grid.Resources>
        <Style x:Key="Edit"/>
        <Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource Edit}" x:Key="ToggleStyle"/>
    </Grid.Resources>

    <RadioButton Style="{StaticResource ToggleStyle}"/>

</Grid>
0

There are 0 best solutions below