WPF 如何处理绑定到空对象的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21066931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:29:05  来源:igfitidea点击:

How does WPF handle binding to the property of a null object?

wpfmultithreadingxamlpropertiesnull

提问by user1807768

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

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

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

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.

绑定到这个 listBox 的是一个模型视图集合,它在单独的线程上加载集合中项目的组件。当 xaml 代码首次由合成引擎呈现时,“PossivelyNullObject”可能未设置为值。

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 如何处理这个问题?它是否使用默认值(没有图像源所以没有图像)并继续?它等待吗?它是否会自动检测值何时初始化并使用新源重新渲染?如果我以编程方式调用“MyProperty.PossivelyNullObject.UrlProperty”,它如何不会以相同的方式抛出对象空异常?当我尝试调用它时,如何在我自己的代码中重现此功能?

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.

感谢您的任何建议。我是 WPF 的新手,令人尴尬,我正试图解决一个超出我深度的问题。图像加载是一个性能问题,所以我找到了一个解决方案来加载、解码,然后在后台线程上冻结图像源,这样它就不会锁定 UI。不幸的是,当我尝试用调用相同属性的解决方案替换图像源绑定时,我遇到了这个空异常问题。WPF 以某种方式处理可能的空对象,我想用同样的方式来保持干净。

回答by Anatoliy Nikolaev

In BindingBasehave two properties: TargetNullValueand FallbackValue.

InBindingBase有两个属性:TargetNullValueFallbackValue

TargetNullValuereturns your value when the value of the source is null.

TargetNullValue当源的值为null时返回您的值。

FallbackValuereturns your value when the binding is unable to return a value.

FallbackValue当绑定无法返回值时返回您的值。

Example of using:

使用示例:

<!-- 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 Property

BindingBase.TargetNullValue 属性

BindingBase.FallbackValue Property

BindingBase.FallbackValue 属性