我在XAML中有一个Image元素.我将Source属性绑定到ViewModel中的字符串属性.但是,有时值为null,然后我在调试窗口中出现错误.
我在这里读到:Source = null的ImageSourceConverter错误,如果值为null,我可以使转换器返回DependencyProperty.UnsetValue.
现在我想知道是否可以直接在XAML中进行操作?也许通过使用FallbackValue?我尝试了一些变种,但没有运气.
这是我在XAML中的Image元素:
<Image Name="img" Source="{Binding Path=CurrentImageSource}" Stretch="None" />
Run Code Online (Sandbox Code Playgroud)
而CurrentImageSource只是DataContext上的一个字符串属性.
错误消息是:System.Windows.Data错误:23:
无法将''从类型''转换为'sv-SE'文化的'System.Windows.Media.ImageSource',默认转换为; 考虑使用Binding的Converter属性.NotSupportedException:'System.NotSupportedException:ImageSourceConverter无法转换(null).
flo*_*dob 54
我使用x:Null和TargetNullValue,这样你就可以获得一个简单的空白图像......
<Image Source="{Binding LogoPath, TargetNullValue={x:Null}}" />
Run Code Online (Sandbox Code Playgroud)
这样您就不需要将Triggers或BitmapImages作为静态资源.
Sas*_*nig 18
您可以使用数据触发器检查空引用:
<Image Name="img" Stretch="None" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding CurrentImageSource}" />
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentImageSource}" Value="{x:Null}">
<Setter Property="Source" Value="/ImageNullRef;component/errorImage.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Run Code Online (Sandbox Code Playgroud)
虽然不可能直接测试两个值是否不同,但您可以使用Mike Hillberg撰写的这种方法来查看值是更大还是更小等.
mbu*_*ill 16
我没有测试过,但我认为这就是TargetNullValue的用途:
<Image Name="img" Source="{Binding Path=CurrentImageSource, TargetNullValue=/ImageNullRef;component/errorImage.png}" Stretch="None" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17483 次 |
最近记录: |