当按钮被禁用时,我想使我的图像(在按钮上)变灰。当我在按钮上有文本(无图像)时,文本显示为灰色(图像作为按钮内容,它们不会显示为灰色)。有没有简单又漂亮的方法来做到这一点?
这是我的 xaml 文件:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ToolBarTray VerticalAlignment="Top" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" IsLocked="true" Grid.Row="0">
<ToolBar Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Band="1" BandIndex="1">
<Button Command="{Binding Button1}" RenderOptions.BitmapScalingMode="NearestNeighbor">
<Button.Content>
<Image Source="open.ico"></Image>
</Button.Content>
</Button>
<Button Command="{Binding Button2}" RenderOptions.BitmapScalingMode="NearestNeighbor">
<Button.Content>
<Image Source="open.ico"></Image>
</Button.Content>
</Button>
</ToolBar>
</ToolBarTray>
</Grid>
</Window>
这是我的文件后面的代码:
public partial class MainWindow : Window
{
private RelayCommand _button1;
private RelayCommand _button2;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public ICommand Button1
{
get
{
if (_button1 == null)
{
_button1 = new RelayCommand(Button1E, Button1C);
}
return _button1;
}
}
public ICommand Button2
{
get
{
if (_button2 == null)
{
_button2 = new RelayCommand(Button2E, Button2C);
}
return _button2;
}
}
public void Button1E(object parameter)
{
Trace.WriteLine("Button 1");
}
public bool Button1C(object parameter)
{
return true;
}
public void Button2E(object parameter)
{
Trace.WriteLine("Button 2");
}
public bool Button2C(object parameter)
{
return false;
}
}