我是xaml的新手。我用相同的事件处理程序制作了2个按钮。所以当我点击一个按钮时,它会向它添加一个图像。问题是当我点击另一个按钮时,前一个按钮上的图像消失。 当我点击另一个按钮时,如何将图像保留在上一个按钮上?
<Window.Resources>
<Image x:Key="image1" Source="folder\image1.png" />
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="185,73,0,0" VerticalAlignment="Top" Width="75" Click="Clickable"/>
<Button x:Name="button_Copy" Content="Button" HorizontalAlignment="Left" Margin="315,73,0,0" VerticalAlignment="Top" Width="75" Click="Clickable"/>
</Grid>
//事件处理程序
private void Clickable(object sender, RoutedEventArgs e)
{
Button a = (Button)sender;
a.Content = FindResource("image1");
}
答案 0 :(得分:0)
试试这个。顺便说一句,我认为写一个新的controlTemplete会更好。
private void Clickable(object sender, RoutedEventArgs e)
{
Button a = (Button)sender;
a.Content = new Image() {Source = new BitmapImage(new Uri(@"folder/image1.png",UriKind.Relative)) };
}
答案 1 :(得分:-1)
你可以试试这个导致一个事件:
private void Clickable(object sender, RoutedEventArgs e)
{
if(sender.Name == "button")
{
button.Content = FindResource("image1");
}
else
{
button_Copy.Content = FindResource("image1");
}
}
答案 2 :(得分:-1)
将x:Shared="False"
添加到您的图片资源。这将创建资源的多个实例。阅读更多