【问题标题】:How to convert ImageSource to bool如何将 ImageSource 转换为布尔值
【发布时间】:2016-05-14 02:41:20
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf xaml ivalueconverter


    【解决方案1】:

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

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

    if (value == null) return false;
    

    【讨论】:

    • 更简单:return value != null;
    【解决方案2】:

    我会改用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,那么您的按钮将被启用。 Commands 的真正好处是您可以在菜单和几乎任何控件上重复使用它们,而按钮的 Click 事件仅对按钮有效。

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

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多