【问题标题】:WPF button background mouseenter and leave not working as expectedWPF按钮背景鼠标进入并离开不按预期工作
【发布时间】:2013-12-16 11:31:10
【问题描述】:

我有一个像这样的 WPF 按钮:

<Page.Resources>
      <ImageBrush x:Key="black_pane_normal" ImageSource="/Images/TroubleShooting/black_pane_clear.png" />
</Page.Resources>

<Button x:Name="ButtonBlackPane" Background="{StaticResource black_pane_normal}" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="157" Height="136" MouseEnter="ButtonBlackPane_MouseEnter" MouseLeave="ButtonBlackPane_MouseLeave" Click="ButtonBlackPane_Click" RenderTransformOrigin="0.533,0.281" Margin="189,199,0,0"/>

我的 C# 代码是:

    BitmapSource _black_pane_yellow_border = Imaging.CreateBitmapSourceFromHBitmap(InstallerToolkit.Properties.Resources.black_pane_yellow.GetHbitmap(),
                                                                          IntPtr.Zero,
                                                                          Int32Rect.Empty,
                                                                          BitmapSizeOptions.FromEmptyOptions());

    BitmapSource _black_pane_no_border = Imaging.CreateBitmapSourceFromHBitmap(InstallerToolkit.Properties.Resources.black_pane_clear.GetHbitmap(),
                                                                          IntPtr.Zero,
                                                                          Int32Rect.Empty,
                                                                          BitmapSizeOptions.FromEmptyOptions());

    private void ButtonBlackPane_MouseEnter(object sender, MouseEventArgs e)
    {
        ButtonBlackPane.Background = new ImageBrush(_black_pane_yellow_border);
    }

    private void ButtonBlackPane_MouseLeave(object sender, MouseEventArgs e)
    {
        ButtonBlackPane.Background = new ImageBrush(_black_pane_no_border);
    }

我的第一个问题是我的图像没有填满整个按钮背景,我该如何让它填满它?

我的第二个问题是当鼠标进入按钮时,正确的背景图像会显示一会儿,然后显示默认的灰色按钮图像并且我的图像消失了,我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    首先,首先使用静态资源设置背景,然后每次使用代码将其设置为新的 ImageBrush 对象,这不是一个好习惯。查看 FindResource 方法 (http://msdn.microsoft.com/de-de/library/system.windows.frameworkelement.findresource(v=vs.110).aspx) 以在代码后面使用您的资源。

    其次,您应该使用样式和触发器来修改控件的外观,例如根据鼠标悬停状态。您可以使用一个名为 IsMouseOver 的属性。这是一个例子:

    <Page.Resources>
        <Style TargetType="Button" x:Key="myButtonStyle">
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="FontSize" Value="12" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Foreground" Value="Blue" />
                    <Setter Property="Cursor" Value="Hand"/>
                    <Setter Property="FontSize" Value="16" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Page.Resources>
    
    <Button Width="200" Height="100" Style="{StaticResource myButtonStyle}">Hello, World!</Button>
    

    第三,由于WPF按钮的默认模板,鼠标悬停时不能直接改变其背景。这个问题在这里讨论和解决:How do you change Background for a Button MouseOver in WPF? 如果您想了解有关设置 WPF 控件样式以及如何使用默认模板的更多信息,请查看此处:http://msdn.microsoft.com/en-us/library/aa970773.aspx

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 2023-03-31
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2019-10-24
      • 2012-04-01
      • 2020-10-20
      • 1970-01-01
      相关资源
      最近更新 更多