Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:
VB
Public Shared Function CreateNavigationPanel(ByVal navigationText As String, ByVal title As String) As DockPanel
            Dim myDock As New DockPanel()
            Dim myButton As New Button()
            'add text into a TextBlock, and add some style
            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
            'Create the triangle
            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
            'Create the rectangle and set default width and height
            Dim rec As New Rectangle()
            rec.Height = 30
            rec.Width = navigationText.Length * 8

            'Set the possition of the triangle
            Canvas.SetTop(myPolygon, 0)
            Canvas.SetLeft(myPolygon, 0)
            'Set the possition of the rectangle
            Canvas.SetTop(rec, 0)
            Canvas.SetLeft(rec, 10)
            'add rectangle and triangle to the Canvas
            myCanvas.Children.Add(myPolygon)
            myCanvas.Children.Add(rec)

            'Set the width and height of the Canvas depending on size of rectangle
            myCanvas.Width = myPolygon.Width + rec.Width
            myCanvas.Height = rec.Height
            'Create some gradiant background
            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()
            'fill Gradiant to triangle
            myPolygon.Fill = myGradiant
            'fill Gradiant to rectangle
            rec.Fill = myGradiant
            'Create a virtual brush of the Canvas
            visual.Visual = myCanvas
            'Add text and background to the panel
            myPanel.Children.Add(mytext)
            myPanel.Background = visual
            'set width and height to the panel depending on Canvas size
            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:

VB
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
Posted
Updated 14-Feb-11 2:01am
v6
Comments
Wayne Gaylard 11-Feb-11 8:18am    
Don't know why someone uni-voted this question. Have a 5 to counter attack.

You don't say wether you are using custom templates in your application. If so, you need to check your triggers in your default button style and make sure that the IsMouseOver and IsPressed property rtiggers values are set appropriately. If you are'nt using a custom template, each control comes with a default template, which you need to alter to create the effects you would like to happen when your users hover over or press the button. Sorry about being so general but you don't give any more info.

Happy Coding

EDIT
Herewith button sample with Template.
XML
<Button Name="btnHelloWorld" Content="Hello World" Width="150" Margin="5" Click="btnInvoice_Click">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Border" CornerRadius="1,8,1,8" BorderThickness="2" Background="Gray" BorderBrush="Black">
                        <Border.Effect>
                            <DropShadowEffect />
                        </Border.Effect>
                        <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="LightGray" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="LightGray" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="White" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>

EDIT
 
Share this answer
 
v2
Comments
Joachim Bjerke 11-Feb-11 5:11am    
Hello zimvbcoder,
Thank you for you answer.
I do not use any custom templates. How do I change the default template on a button?

Thanks
Wayne Gaylard 11-Feb-11 8:09am    
Hi Joachim,
You would need to create your own Template using <Button.Template> in your button declaration. In your Template create a ControlTemplate and set it's TargetType to Button, then you can set your ControlTemplate.Triggers to the appropriate settings. I will post a sample in my original answer. My suggestion would be to read up on Custom Styles and Templates in WPF, as you could more than likely create everything you want to do in XAML without using any of that code behind!
Use Transparent Image Instead of Button and activate it with mouse down event you can get Transparent Image From Google Images just find for spacer.
your Xmal code willbe

<Image Height="12" HorizontalAlignment="Left" Margin="507,102,0,0" Name="Image1" MouseDown="Image1_Click"/>

And your Xmal.cs code will be

public void Image1_Click(object sender, RoutedEventArgs e)
{
Window1 wind = new Window1();
wind.Owner = this;
wind.Show();
}
 
Share this answer
 
Comments
Stevens Miller 19-Apr-17 10:10am    
You won't get a mouse-down event if you use the spacebar to click a button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900