C# 你是怎么按按钮的
我在C#中有一个WPF应用程序。当我运行应用程序时,它会动态创建一个带有背景图像的按钮 这是示例代码:C# 你是怎么按按钮的,c#,.net,wpf,button,C#,.net,Wpf,Button,我在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.S
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>
但它不起作用。您必须为控件编写样式,以便在其生命周期(鼠标悬停/退出/单击/聚焦/等等)中保留背景图像。一旦你习惯了,其实并不难,所以不用害怕:) 这些链接应能让您立即了解: 试试这段代码
<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"/>
我发现在C#表单应用程序中有一个属性“UseVisualStyleBackgroundColor”,如果该属性为UseVisualStyleBackgroundColor=false;我解决了我的问题,但在WPF应用程序中没有这个属性。我是怎么修好的?
<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"/>