Hello, I wonder if someone have some experience with background for System.Windows.Conotrols.Button ? In my WPF project I have a button, where I have added some drawing:
Public Shared Function CreateNavigationPanel(ByVal navigationText As String, ByVal title As String) As DockPanel
Dim myDock As New DockPanel()
Dim myButton As New Button()
Dim mytext As New TextBlock()
mytext.Text = navigationText
mytext.Foreground = Brushes.White
mytext.Background = Brushes.Transparent
mytext.Margin = New Thickness(12, 6, 0, 0)
mytext.FontWeight = FontWeights.Bold
Dim myPanel As New StackPanel()
Dim visual As New VisualBrush()
Dim myCanvas As New Canvas()
myCanvas.Background = Brushes.Transparent
Dim myPolygon As New Polygon()
myPolygon.Stroke = Brushes.Black
myPolygon.Fill = Brushes.LightSeaGreen
myPolygon.StrokeThickness = 0
myPolygon.HorizontalAlignment = HorizontalAlignment.Left
myPolygon.VerticalAlignment = VerticalAlignment.Top
Dim Point1 As New System.Windows.Point(10, 0)
Dim Point2 As New System.Windows.Point(0, 15)
Dim Point3 As New System.Windows.Point(10, 30)
Dim myPointCollection As New PointCollection()
myPointCollection.Add(Point1)
myPointCollection.Add(Point2)
myPointCollection.Add(Point3)
myPolygon.Points = myPointCollection
myPolygon.Width = 20
myPolygon.Height = 30
Dim rec As New Rectangle()
rec.Height = 30
rec.Width = navigationText.Length * 8
Canvas.SetTop(myPolygon, 0)
Canvas.SetLeft(myPolygon, 0)
Canvas.SetTop(rec, 0)
Canvas.SetLeft(rec, 10)
myCanvas.Children.Add(myPolygon)
myCanvas.Children.Add(rec)
myCanvas.Width = myPolygon.Width + rec.Width
myCanvas.Height = rec.Height
Dim myGradiant As New LinearGradientBrush()
myGradiant.StartPoint = New Point(0, 0)
myGradiant.EndPoint = New Point(0, 1)
myGradiant.GradientStops.Add(New GradientStop(Colors.Gray, 0.0))
myGradiant.GradientStops.Add(New GradientStop(Colors.Black, 1.0))
Dim style As New Style()
myPolygon.Fill = myGradiant
rec.Fill = myGradiant
visual.Visual = myCanvas
myPanel.Children.Add(mytext)
myPanel.Background = visual
myPanel.Width = myCanvas.Width
myPanel.Height = myCanvas.Height
Dim myTitle As New TextBlock()
myTitle.Text = title
myTitle.Foreground = Brushes.White
myTitle.FontSize = 18
myTitle.FontWeight = FontWeights.Bold
myTitle.TextAlignment = TextAlignment.Center
myButton.Background = Brushes.Transparent
myButton.Content = myPanel
DockPanel.SetDock(myButton, Dock.Left)
myDock.Children.Add(myButton)
myDock.Children.Add(myTitle)
myDock.Margin = New Thickness(2, 5, 5, 5)
Return myDock
End Function
As you can see, I try to set the button, transparent, and no border, but when I hover or click the button the background changes to the default background style. I dont want the background to change when click or hover the button. Can someone please help me with this? Thanks.
EDIT
Hi zimvbcoder,
thanks for you answer.
I just have a question. I like to do this in code-behind or in my case a utility class that generates this button. And then I have tried to do something like this:
Dim template As New ControlTemplate(GetType(Button))
Dim trigger As New Trigger()
trigger.Property = Button.IsKeyboardFocusedProperty
trigger.Value = True
trigger.Setters.Add(New Setter(Button.BorderBrushProperty, Brushes.Blue))
template.Triggers.Add(trigger)
trigger = New Trigger()
trigger.Property = Button.IsMouseOverProperty
trigger.Value = True
trigger.Setters.Add(New Setter(Button.BackgroundProperty, Brushes.Blue))
template.Triggers.Add(trigger)
trigger = New Trigger()
trigger.Property = Button.IsPressedProperty
trigger.Value = True
trigger.Setters.Add(New Setter(Button.BackgroundProperty, Brushes.Blue))
trigger.Setters.Add(New Setter(Button.BorderBrushProperty, Brushes.Green))
template.Triggers.Add(trigger)
Button1.Template = template
When I run this application, the button is not shown, so I think I miss something in the template object.
EDIT