2
votes
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:CuratioCMS.Client.UI.Controls">
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type local:ImageButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ImageButton}">
                <StackPanel Height="Auto" Orientation="Horizontal">
                    <Image Width="20"
                           Height="20"
                           Margin="10,4,0,4"
                           Source="{Binding Path=Image,
                                            RelativeSource={RelativeSource TemplatedParent}}"
                           Stretch="Fill" />
                    <TextBlock Margin="5,0,10,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               FontSize="12"
                               FontWeight="Bold"
                               Foreground="{TemplateBinding Foreground}"
                               Text="{TemplateBinding Label}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

this is the code inside Generic.xaml for ImageButton custom control. whit works as expected but I can not inherit from Base button stale so instead of inheriting all base styling from Button this style only creates custom control without any base styles and as far as

BasedOn="{StaticResource {x:Type Button}}"

inside VS with line shows error which says Resource '{x:Type System.Windows.Controls.Button}' is not found

I do not know how to achieve desired styling and why this error message is there showing up inside Visual studio editor

1

1 Answers

0
votes

You are defining a style based on a generic button style (which is inside Generic.xaml) and then redefining the template of the style. this way the base style will be overwritten.

if you wish to change the style a bit, you got two options:

option1:

  1. provide a key for the generic button style (find the style defined within Generic.xaml with TargetType="{x:Type Button}" and add x:Key="someName" to its properties)
  2. set the style of every other buttons (except for ImageButtons) to {StaticResource someName}
  3. set the BasedOn property of your ImageButton style to {StaticResource someName}

option2:

  1. you can make a copy of the generic button style
  2. leave it generic by setting its TargetType to ImageButton and 'not' setting any key for it
  3. change the desired parts of it.

option 2 is generally a better solution since it does not require lots of change, but I've been through what you wanted to do some time ago and I didn't find any way to avoid duplicated style code.