【发布时间】:2016-05-14 02:41:20
【问题描述】:
我有Image 和Button 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