C# ImageButton:禁用按钮时不显示图像
我创建了一个UserControl来实现一个简单的C# ImageButton:禁用按钮时不显示图像,c#,wpf,xaml,user-controls,imagebutton,C#,Wpf,Xaml,User Controls,Imagebutton,我创建了一个UserControl来实现一个简单的ImageButton,如下所示: <UserControl x:Class="MyApp.Common.Controls.ImageButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ImageButton
,如下所示:
<UserControl x:Class="MyApp.Common.Controls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="UC">
<Grid>
<Button Command="{Binding ElementName=UC, Path=Command}" CommandParameter="{Binding ElementName=UC, Path=CommandParameter}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=UC, Path=Image}"
Width="{Binding ElementName=UC, Path=ImageWidth}"
Height="{Binding ElementName=UC, Path=ImageHeight}"/>
<TextBlock Text="{Binding ElementName=UC, Path=Text}" />
</StackPanel>
</Button>
</Grid>
用法很简单:
<cc:ImageButton Command="{Binding SearchCommand}"
Image="/MyApp;component/Images/magnifier.png"
ImageWidth="16" ImageHeight="16"
/>
当按钮(绑定到我的ViewModel中的DelegateCommand)被禁用时,图像将消失。否则,所有工作都会按预期进行。
有什么问题吗?
禁用时如何使图像以灰度显示
更新:这是这种奇怪行为的图像:我会将其设置为自定义控件,在
属性=isEnabled值=false处为其设置触发器。在图像上,我会添加一个带有一些不透明度的网格,并使用该触发器控制该不透明度或可见性。祝你好运我认为没有更简单的方法。我复制了您提供的代码,看看是否可以重现您遇到的问题。不幸的是,当命令被禁用时(或者它的CanExecute
返回false
),我使用的图像没有消失。请从您的ViewModel
中提供您认为可能相关的更多代码,好吗
回答问题的第二部分:
禁用时如何使图像以灰度显示
据我所知,在WPF中没有简单的方法去饱和图像。相反,我会采用@Vova建议的方法,即降低图像的不透明度。您可以修改您提供的UserControl XAML,如下所示:
<UserControl x:Class="MyApp.Common.Controls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="UC">
<Grid>
<Button Command="{Binding ElementName=UC, Path=Command}" CommandParameter="{Binding ElementName=UC, Path=CommandParameter}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=UC, Path=Image}"
Width="{Binding ElementName=UC, Path=ImageWidth}"
Height="{Binding ElementName=UC, Path=ImageHeight}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}} }" Value="false" >
<Setter Property="Opacity" Value="0.3" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding ElementName=UC, Path=Text}" />
</StackPanel>
</Button>
</Grid>
</UserControl>
在上面的代码中,如果按钮的IsEnabled
属性等于false
,我在图像的Style
属性中添加了DataTrigger
,以修改图像的不透明度
希望这有帮助 如前所述,WPF中没有内置的去饱和支持,但您可以轻松地使用来实现这一点。该链接将带您访问一个实现,该实现允许您仅以XAML方式使用该效果
请注意,对于用HLSL编写的着色器,需要进行编译,对于编译,您需要在计算机上安装Direct X SDK。他承认,对于这么小的任务来说,这简直是一个怪兽。ViewModel代码非常简单。private bool CanSearchAccount(){return!string.IsNullOrWhiteSpace(SearchValue);}。数据库是否调用并填充searchResults ObservableCollection SearchAccount方法图像和文本块是否都消失了?还是仅仅是图像?图像和文本都消失了。见附件截图
<UserControl x:Class="MyApp.Common.Controls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="UC">
<Grid>
<Button Command="{Binding ElementName=UC, Path=Command}" CommandParameter="{Binding ElementName=UC, Path=CommandParameter}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=UC, Path=Image}"
Width="{Binding ElementName=UC, Path=ImageWidth}"
Height="{Binding ElementName=UC, Path=ImageHeight}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}} }" Value="false" >
<Setter Property="Opacity" Value="0.3" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding ElementName=UC, Path=Text}" />
</StackPanel>
</Button>
</Grid>
</UserControl>