【问题标题】:How intercept onMouseOver on the Button如何拦截Button上的onMouseOver
【发布时间】:2014-01-07 14:57:22
【问题描述】:

我有一个 C# 中的 WPF 应用程序。当我运行我的应用程序时,它会动态创建一个带有背景图像的按钮。

这是示例代码:

Button button = new Button();
button.Width = 130;
button.Height = 190;
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(Directory.GetCurrentDirectory()+@"\Images\nao_grigio.png")));
brush.Stretch = Stretch.Uniform;
buttonCoppia.Background = brush;
button.Background = brush;

它有效,所以当我用鼠标传递按钮时,图像会更改为默认按钮图像。

我是怎么解决的?


我已将您的代码复制到 XAML 文件中。所以我的代码是

<Window x:Name="idWindows" x:Class="MemoryGame.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800">
<Window.Resources>
    <BitmapImage x:Key="ImageSource" UriSource="Resources\nao_grigio.png"/>
    <BitmapImage x:Key="MouseOverImage" UriSource="Resources\nao_grigio.png"/>
    <BitmapImage x:Key="MousePressed" UriSource="Resources\nao_grigio.png"/>
    <Style x:Key="BtnStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="Gd">
                        <Grid.Background>
                            <ImageBrush ImageSource="{StaticResource ResourceKey= ImageSource}" Stretch="Fill"></ImageBrush>
                        </Grid.Background>
                        <ContentPresenter></ContentPresenter>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="Gd">
                                <Setter.Value>
                                    <ImageBrush ImageSource="{StaticResource ResourceKey= MouseOverImage}" Stretch="Fill"></ImageBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Button x:Key="MyStyle" Style="{StaticResource BtnStyle}" Height="100" Width="300"/>
</Window.Resources>
<Grid>
    <Label Content="Memory" HorizontalAlignment="Left" Margin="320,10,0,0" VerticalAlignment="Top" FontSize="40"/>
    <Label x:Name="labelTime" Content="Tempo : " HorizontalAlignment="Left" Margin="628,136,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.211,-0.115"/>
    <Label x:Name="labelSecondi" Content="0 sec" HorizontalAlignment="Right" Margin="0,136,10,0" VerticalAlignment="Top"/>
    <Label x:Name="labelNumeroErrori" Content="Numero Errori : " HorizontalAlignment="Left" Margin="628,178,0,0" VerticalAlignment="Top"/>
    <Label x:Name="labelNumeroErrori_Cont" Content="0" HorizontalAlignment="Right" Margin="0,178,12,0" VerticalAlignment="Top" RenderTransformOrigin="-0.125,-0.692"/>
    <Label x:Name="labelNumeroRisposteNonDate" Content="Risposte non date : " HorizontalAlignment="Left" Margin="628,220,0,0" VerticalAlignment="Top"/>
    <Label x:Name="labelNumeroRisposteNonDate_Cont" Content="0" HorizontalAlignment="Right" Margin="0,220,12,0" VerticalAlignment="Top" RenderTransformOrigin="-0.125,-0.692"/>
    <Grid x:Name="gridToken"  HorizontalAlignment="Left" Height="400" Margin="72,136,0,0" VerticalAlignment="Top" Width="500"/>
</Grid>

在 MainWindow.xaml.cs 我有这个:

 Button button = new Button();
     ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(Directory.GetCurrentDirectory()+@"\Images\nao_grigio.png")));
   brush.Stretch = Stretch.Uniform;
   buttonCoppia.Background = brush;
   button.Background = brush;
   button.Style = this.Resources["MyStyle"] as Style;

但它不起作用。

【问题讨论】:

  • 我在 C# Form 应用程序中看到有这个属性“UseVisualStyleBackgroundColor”,如果这个属性是 UseVisualStyleBackgroundColor=false;我解决了我的问题,但是在 WPF 应用程序中没有这个属性。我是如何解决的?

标签: c# .net wpf button


【解决方案1】:

您必须为控件编写样式,以便在其生命周期(鼠标悬停/退出/单击/聚焦/等...)期间保留背景图像。习惯了其实并不难,所以不用害怕:)

这些链接应该让您立即了解:

How do I style Buttons

http://wpftutorial.net/Styles.html

http://msdn.microsoft.com/en-us/library/aa970773%28v=vs.110%29.aspx

【讨论】:

    【解决方案2】:

    试试这个代码

      <BitmapImage x:Key="ImageSource" UriSource="DefaultImage.png"/>
        <BitmapImage x:Key="MouseOverImage" UriSource="MouseOverImage.png"/>
        <BitmapImage x:Key="MousePressed" UriSource="MousePressed.png"/>
    
        <Style x:Key="BtnStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="Gd">
                            <Grid.Background>
                                <ImageBrush ImageSource="{StaticResource ResourceKey= ImageSource}" Stretch="Fill"></ImageBrush>
                            </Grid.Background>
                            <ContentPresenter></ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Button.IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="Gd">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="{StaticResource ResourceKey= MouseOverImage}" Stretch="Fill"></ImageBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="Button.Pressed" Value="False">
                                <Setter Property="Background" TargetName="Gd">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="{StaticResource ResourceKey= MousePressed}" Stretch="Fill"></ImageBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    
    <Button Style="{StaticResource BtnStyle}" Height="100" Width="300"/>
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多