2014-01-07 71 views
0

我在C#中有一個WPF應用程序。當我運行我的應用程序時,它動態地創建一個帶背景圖像的按鈕如何攔截按鈕上的鼠標過濾

這是示例代碼:

Button button = new Button(); 
button.Width = 130; 
button.Height = 190; 
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(Directory.GetCurrentDirectory()[email protected]"\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()[email protected]"\Images\nao_grigio.png"))); 
    brush.Stretch = Stretch.Uniform; 
    buttonCoppia.Background = brush; 
    button.Background = brush; 
    button.Style = this.Resources["MyStyle"] as Style; 

但它不工作。

+0

我已經看到在C#Form應用程序中有這個屬性「UseVisualStyleBackgroundColor」,如果這個屬性是UseVisualStyleBackgroundColor = false;我解決了我的問題,但在WPF應用程序中沒有這個屬性。我如何修復它? – bircastri

回答

0

試試這個代碼

<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"/>