单击并悬停时WPF背景发生变化 [英] WPF background changes when click and hover

查看:70
本文介绍了单击并悬停时WPF背景发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道是否有人对System.Windows.Conotrols.Button的背景有所了解?在我的WPF项目中,我有一个按钮,在其中添加了一些图形:

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()
            '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


如您所见,我尝试将按钮设置为透明且无边框,但是当我将鼠标悬停或单击按钮时,背景将更改为默认背景样式.我不希望单击或将按钮悬停时更改背景.有人可以帮我吗?谢谢.





编辑

zimvbcoder您好,
谢谢你的回答.
我只是有一个问题.我喜欢在代码隐藏中执行此操作,或者在我的情况下喜欢生成此按钮的实用程序类.然后我试图做这样的事情:


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



当我运行此应用程序时,未显示该按钮,因此我想我错过了模板对象中的某些内容.

EDIT



When I run this application, the button is not shown, so I think I miss something in the template object.

EDIT

推荐答案

您不会说您是否正在应用程序中使用自定义模板.如果是这样,则需要以默认按钮样式检查触发器,并确保正确设置了IsMouseOver和IsPressed属性rtiggers值.如果您不使用自定义模板,则每个控件都带有一个默认模板,您需要对其进行更改以创建您希望在用户将鼠标悬停或按下按钮时会发生的效果.抱歉这么笼统,但您不提供更多信息.

快乐编码

编辑
随函附上范本的范本.
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.
<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>


编辑


使用透明图像而不是按钮,并通过鼠标按下事件将其激活,您可以从Google图片中获取透明图像,只需查找间隔物即可.
您的Xmal代码将

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

而您的Xmal.cs代码将是

公共无效Image1_Click(对象发送者,RoutedEventArgs e)
{
Window1风= new Window1();
wind.Owner =此;
wind.Show();
}
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();
}


这篇关于单击并悬停时WPF背景发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆