我创建了一个WPF应用程序。在那我需要在按钮上显示图像。那么我应该在哪里将该图像存储在我的应用程序中以及如何设置图像的源属性? 请重播
答案 0 :(得分:0)
<Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400">
<Button.Background>
<ImageBrush **ImageSource ="Images/AERO.png"** ></ImageBrush>
</Button.Background>
</Button>
在CS文件中:(将图像存储在资源或项目目录中的任何文件夹中)
private void Button1_Click_1(object sender, RoutedEventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
Button1.Background = brush;
}
答案 1 :(得分:0)
<Grid>
<Button x:Name="button" HorizontalAlignment="Left" Margin="190,136,0,0" VerticalAlignment="Top" Width="104" Height="42">
<Image Source="pen.png" />
</Button>
</Grid>
答案 2 :(得分:0)
您可以在按钮上添加图片,例如
<Button>
<StackPanel>
<Image Source="Images/imageName.png" />
</StackPanel>
</Button>
否则你可以尝试,
<Window.Resources>
<ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" />
</Window.Resources>
<Grid>
<Button Background="{StaticResource MyResource}"/>
</Grid>
否则试试这个。
<Window.Resources>
<ImageBrush x:Key="MyResource" ImageSource="Images/imageName.png" />
</Window.Resources>
<Grid>
<Button>
<Button.Content>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="34" Height="19" Margin="0">
<TextBlock.Background>
<StaticResource ResourceKey="MyResource"/>
</TextBlock.Background>
</TextBlock>
</Button.Content>
</Button>
</Grid>
答案 3 :(得分:0)
将图像添加到项目并设置图像属性:
然后在.xaml文件中添加一个Button(例如):
<Button HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="40" Height="40">
<Image Source="Resources/yourImageName.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>