如何将ImageSource转换为bool

时间:2016-02-04 13:06:37

标签: c# wpf xaml ivalueconverter

我有ImageButton SaveToDisk。因此,当Image未加载时(ImageSource为null) - 我需要阻止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>

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

输出错误:

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? 谢谢!

2 个答案:

答案 0 :(得分:1)

看起来null不是ImageSource的有效值。 尝试更改

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

if (value == null) return false;

答案 1 :(得分:1)

我会改用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但是要显示命令的使用我认为这样做会很好。