views:

32

answers:

1

Using the following code btn2 looks different to btn1 and btn3 yet they are using exactly the same image.

Public Class MyToolBar
    Inherits ToolBar

    Public Sub New()
        MyBase.New()

        Dim bmp1 = New BitmapImage(ImageUri.DeleteItem)
        Dim bmp2 = New BitmapImage(ImageUri.DeleteItem)
        Dim bmp3 = New BitmapImage(ImageUri.DeleteItem)
        Dim img1 = New Image With {.Source = bmp1}
        Dim img2 = New Image With {.Source = bmp2}
        Dim img3 = New Image With {.Source = bmp3}

        Dim btn1 = New Button With {.Content = img1}
        Dim btn2 = New MyButton With {.Content = img2}
        Dim btn3 = New Button With {.Content = img3}

        Me.AddChild(btn1)
        Me.AddChild(btn2)
        Me.AddChild(btn3)

    End Sub

End Class

Public Class MyButton
    Inherits Button
End Class

The only difference in the code is btn2 is constructed with MyButton. MyButton is a simple inheritance of Button, no other code.

btn1 & btn3 appear as expected; flat. btn2 appears semi raised and has a black border.

The order I add the buttons to the toolbar makes no difference. That is - the weird appearance of btn2 moves to the location that it is added to the toolbar.

I've dumped and compared the properties, to 2 levels, of btn1 and btn2 and there is only the expected differences except for:

btn1.DependencyObjectType.IsSecurityCritical=False btn2.DependencyObjectType.IsSecurityCritical=True

and

btn1.DependencyObjectType.IsSecurityTransparent=True btn2.DependencyObjectType.IsSecurityTransparent=False

Anything else I should look into?

+2  A: 

Check you XAML for a default style that applies to MyButton.

Update: The standard WPF themes include default styles for well-known controls. Your MyButton is not one of those. You need to extend the theme dictionary with style for your class, or you need to declare a default style for your class that is based on the Button style.

Here's the simplest style you can try adding to your resource dictionary (where local: is the XAML declaration of your CLR namespace - you need to add this to your root XAML element)

<Style BasedOn="{StaticResource {x:Type Button}}"
       TargetType="{x:Type local:MyButton}" />

Update 2: If your button is part of a toolbar, try this one instead (:-P):

<Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
       TargetType="{x:Type local:MyButton}" />
Franci Penov
Tim Murphy
@Franci. Thanks so much for your help. Your style is correct for buttons but as the button is a part of a toolbar the correct style is <Style TargetType="{x:Type local:MyButton}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" />. When you change your accordingly I'll accept the answer. Thanks again.
Tim Murphy