如何将 ImageSource 转换为 bool

How to convert ImageSource to bool

我有 ImageButton 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:

  <UserControl.Resources>
    <converters:SourceToEnableConverter x:Key="SourceToEnableConverter"/>
</UserControl.Resources>

和WPFXAML图像:

 <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}}"
                    />

并且输出错误:

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

你能帮帮我吗?也许我应该用别的东西?或者,我以错误的方式使用 IValueConverter? 谢谢!

null 似乎不是 ImageSource 的有效值。 尝试更改

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

if (value == null) return false;

我会改用 Command。使用CanExecute方法判断按钮是否启用,像这样:

XAML

    <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;
    }
}

如果 CanExecutetrue 那么您的按钮将被启用。命令的真正好处是您可以在菜单和几乎所有控件上重复使用它们,而按钮的 Click 事件仅对按钮有效。

我通常会使用 DataContext,但为了展示命令的使用,我认为这会很好。