WPF按钮背景问题

时间:2013-06-12 10:32:53

标签: c# wpf button

我有一个有25个按钮的网格控件(网格只包含按钮)。对于每个按钮,我设置相同的图像:

     ImageBrush brush = new ImageBrush();
     BitmapImage bitmap = new BitmapImage();
     bitmap.BeginInit();
     bitmap.UriSource = new Uri(@"pack://application:,,,/Images/notexplored.jpg", UriKind.RelativeOrAbsolute);
     bitmap.EndInit();
     brush.ImageSource = bitmap;

     foreach (UIElement uie in myGrid.Children)
     {
        var temp = uie as Button;
        temp.Background = brush;
     }

我的问题是,现在当我将鼠标悬停在按钮上时我得到了这个(这是一个带有问题的简短视频):https://www.dropbox.com/s/jb8fb29lrpimue5/ButtonIssue.avi

我怎么能纠正这个?将图像作为背景应用于按钮后,如果我将鼠标悬停在按钮上,我不希望看到默认按钮背景(外观和感觉)而不是应用图像。

4 个答案:

答案 0 :(得分:3)

为了便于重新创建样本,我在此处将“StackPanel”作为容器。如果您不需要动态设置图像,我希望应用如下所示的样式应该获得带有外观和效果的按钮。感觉如你所愿,同时仍然允许使用事件等,就像使用典型的WPF按钮一样。

<StackPanel x:Name="sp">
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}" x:Key="btnWithImageBgStyle">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border CornerRadius="5" BorderBrush="{TemplateBinding BorderBrush}" >
                                <Border.Background>
                                    <ImageBrush ImageSource="pack://application:,,,/Tulip.jpg"/>
                                </Border.Background>
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
        <Button x:Name="btn1" Width="100" Height="50" Content="Button1!" Style="{StaticResource btnWithImageBgStyle}" Click="btn1_Click"/>
        <Button x:Name="btn2" Width="100" Height="50" Content="Button2!" Style="{StaticResource btnWithImageBgStyle}"/>
        <Button x:Name="btn3" Width="100" Height="50" Content="Button3!" Style="{StaticResource btnWithImageBgStyle}"/>

答案 1 :(得分:1)

不是设置背景,只需添加新图像作为按钮的Content。一个小例子:

var b = new Button();

var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"pack://application:,,,/floor.png", UriKind.RelativeOrAbsolute);
bitmap.EndInit();

var img = new Image {Source = bitmap};

b.Content = img;

答案 2 :(得分:1)

您必须更改按钮的控件模板,以便它不再响应MouseOver事件。最简单的解决方案就是这样的

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

这是一种隐式样式,适用于驻留在窗口中的每个按钮。在控件模板中,您可以更改按钮的外观以及状态更改时的行为(例如,当鼠标位于按钮上时)。

通常情况下,控制模板会有更多的代码,但在这个例子中,你只想拥有一个显示某些东西的按钮,但却遗漏了当鼠标结束时或者什么时候可以看到的所有其他行为内容。点击。您可以在MSDN上查看WPF中按钮的默认模板:http://msdn.microsoft.com/en-us/library/ms753328.aspx

我绝对建议您在WPF中了解有关样式和模板的更多信息。这是一个很好的起点:http://msdn.microsoft.com/en-us/library/ms745683.aspx

如果您有任何疑问,请随时发表评论。

答案 3 :(得分:1)

如果您只是需要显示图像并响应点击,那么按钮就会过度。您只能使用ContentControl