1

我需要设置禁用按钮的背景。

private void Button1_Click_1(object sender, RoutedEventArgs e)    
{
     Button1.IsEnabled = false;
     var brush = new ImageBrush();
     brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
     Button1.Background = brush;
}

如果我这样做,我将禁用没有任何背景的按钮。我整个上午都在寻找答案,但没有得到我想要的。我找到了如何在 XAML 中设置禁用按钮的背景,但在 C# 中没有。

4

2 回答 2

0

您不能直接执行此操作,但可以像我的以下代码一样执行此操作:

XMAL 代码

<Button Name="button1"  Click="button1_Click" >
    <Image Name="btndisbackground" Visibility="Hidden" VerticalAlignment="Stretch"
           HorizontalAlignment="Stretch" Source="D:\1.jpg" Stretch="Fill"/>
</Button>

按钮代码

private void button1_Click(object sender, RoutedEventArgs e)
{
    button1.IsEnabled = flase;
    btndisbackground.Visibility = Visibility.Visible;
}
于 2013-06-09T15:28:03.513 回答
0

只需颠倒您做事的顺序。将按钮的启用状态设置为您做的最后一件事:

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;
     Button1.IsEnabled = false;
}

这将更新背景图像,然后禁用该按钮。

于 2013-06-09T15:51:52.733 回答