WPF如何处理对空对象属性的绑定? [英] How does WPF handle binding to the property of a null object?

查看:186
本文介绍了WPF如何处理对空对象属性的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用itemTemplate的listBox,其中包含以下行:

I have a listBox using an itemTemplate that contains the following line:

<Image Source="{Binding MyProperty.PossiblyNullObject.UrlProperty}"/> 

绑定到此listBox的是一个模型视图集合,该模型视图集合在单独的线程上加载集合中项目的组件.合成引擎首次渲染xaml代码时,"PossfullyNullObject"可能不会设置为一个值.

Bound to this listBox is a model view collection that loads components of the items in the collection on a separate thread. The 'PossiblyNullObject' may not be set to a value when the xaml code is first rendered by the composition engine.

WPF如何处理此问题?是否使用默认值(没有图像源,所以没有图像)并继续?等待吗?它会自动检测该值何时初始化并使用新来源重新渲染吗?它不会像我以编程方式调用"MyProperty.PossfullyNullObject.UrlProperty"时一样,引发对象空异常吗?尝试调用此功能时,如何在自己的代码中重现此功能?

How does WPF handle this? Does it use a default value(no image source so no image) and continue on? Does it wait? Does it automatically detect when the value is initialized and rerenders with the new source? How does it not throw object null exceptions in the same way it would if I called 'MyProperty.PossiblyNullObject.UrlProperty' programmatically? How can I reproduce this functionality in my own code when I try to call it?

感谢您的任何建议.对于WPF来说,我是一个令人尴尬的新人,我正在尝试深入解决问题.图像加载是一个性能问题,因此我找到了一种在后台线程中加载,解码然后冻结图像源的解决方案,这样就不会锁定UI.不幸的是,当我尝试用调用相同属性的解决方案替换图像源绑定时,遇到了此null异常问题. WPF以某种方式处理可能的空对象,我想以保持事物整洁的相同方式进行操作.

Thanks for any suggestions. I am embarrassingly new to WPF and I'm trying to tackle a problem out of my depth. The image load is a perf problem so I found a solution to load, decode, then freeze the image source on a background thread so it wouldn't lock up the UI. Unfortunately, I ran across this null exception problem when I tried replacing the image source binding with my solution that calls the same property. WPF somehow handles the possible null objects and I'd like to do it the same way to keep things clean.

推荐答案

BindingBase中具有两个属性:TargetNullValueFallbackValue.

In BindingBase have two properties: TargetNullValue and FallbackValue.

TargetNullValue返回您的值.

FallbackValue返回您的值.

使用示例:

<!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" -->

<Window.Resources>
    <!-- Test data -->
    <local:TestDataForImage x:Key="MyTestData" />

    <!-- Image for FallbackValue -->
    <sys:String x:Key="ErrorImage">pack://application:,,,/NotFound.png</sys:String>

    <!-- Image for NULL value -->
    <sys:String x:Key="NullImage">pack://application:,,,/NullImage.png</sys:String>
</Window.Resources>

<Grid DataContext="{StaticResource MyTestData}">
    <Image Name="ImageNull"
           Width="100" 
           Height="100"
           Source="{Binding Path=NullString, TargetNullValue={StaticResource NullImage}}" />

    <Image Name="ImageNotFound"
           Width="100" 
           Height="100" 
           Source="{Binding Path=NotFoundString, FallbackValue={StaticResource ErrorImage}}" />
</Grid>

有关更多信息,请参见此链接:

See this links, for more information:

BindingBase.TargetNullValue属性

BindingBase.FallbackValue属性

这篇关于WPF如何处理对空对象属性的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆