C# 如何将ImageSource转换为bool

C# 如何将ImageSource转换为bool,c#,wpf,xaml,ivalueconverter,C#,Wpf,Xaml,Ivalueconverter,我有Image和按钮SaveToDisk。所以,当未加载图像时(ImageSource为空)-我需要阻止SaveToDisk按钮。反之亦然 所以,我决定使用转换器。但这对我不起作用: public class SourceToEnableConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.Cultur

我有
Image
按钮SaveToDisk
。所以,当未加载图像时(ImageSource为空)-我需要阻止
SaveToDisk
按钮。反之亦然

所以,我决定使用转换器。但这对我不起作用:

 public class SourceToEnableConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((ImageSource)value == null) return false;

        return true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}
Xaml:


和WPF XAML图像:

 <Image Name="imIcon" Grid.Row="1" Grid.Column="2" 
                   HorizontalAlignment="Left" VerticalAlignment="Center" Margin="8,0,0,8"
                   >
                <Image.Source>
                    <Binding Path="ObjectViewModel.Image" >

                        <!--<Binding.TargetNullValue>
                            <Image Source="Empty.png"></Image>
                        </Binding.TargetNullValue>-->
                    </Binding>
                </Image.Source>                                                         
            </Image>

按钮:

 <Button Name="btnSaveIcon" Click="btnSaveIcon_Click"
                    Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6" Margin="8,0,0,8"
                    HorizontalAlignment="Left" VerticalAlignment="Center"
                    Content="SaveAs"}"
                    Height="44" 
                    IsEnabled="{Binding ElementName=imIcon,Path=Source,Converter={StaticResource SourceToEnableConverter}}"
                    />

看起来null不是ImageSource的有效值。
试着改变

if ((ImageSource)value == null) return false;


我会使用
命令
。使用
CanExecute
方法确定按钮是否已启用,如下所示:

XAML 如果
CanExecute
true
,则您的按钮将被启用。命令的真正好处在于,您可以在菜单和几乎任何控件上重用它们,而按钮的单击事件仅对按钮有效


我通常会使用一个
DataContext
,但是为了显示命令的使用,我认为这样做会很好。

更简单:
返回值!=无效
System.Windows.Data Error: 23 : Cannot convert '<null>' from type '<null>' to  type 'System.Windows.Media.ImageSource' for 'ru-RU' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException
if ((ImageSource)value == null) return false;
if (value == null) return false;
    <Window x:Class="ImageDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ImageDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <RoutedCommand x:Key="LoadImage"/>
        <RoutedCommand x:Key="UnloadImage"/>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource LoadImage}" CanExecute="LoadImage_CanExecute" Executed="LoadImage_Executed"/>
        <CommandBinding Command="{StaticResource UnloadImage}" CanExecute="UnloadImage_CanExecute" Executed="UnloadImage_Executed"/>
    </Window.CommandBindings>
    <DockPanel Margin="5">
        <Image x:Name="imgPlaceholder" Height="100" Width="100" DockPanel.Dock="Top"/>
        <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Top">
            <Button Content="Load Image" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Command="{StaticResource LoadImage}"/>
            <Button Content="Unload Image" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Command="{StaticResource UnloadImage}"/>
        </StackPanel>
    </DockPanel>
</Window>
public partial class MainWindow : Window
{
    public BitmapImage bitmap;
    public MainWindow()
    {
        InitializeComponent();

        bitmap = new BitmapImage();

        bitmap.BeginInit();
        bitmap.UriSource = new Uri("C:\\Temp\\the_ugly_baby.jpg", UriKind.Absolute);
        bitmap.EndInit();
        imgPlaceholder.Source = null;

    }

    private void LoadImage_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        imgPlaceholder.Source = bitmap;
    }

    private void LoadImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = imgPlaceholder.Source == null ? true : false;
    }

    private void UnloadImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = imgPlaceholder.Source != null ? true : false;
    }

    private void UnloadImage_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        imgPlaceholder.Source = null;
    }
}