问题描述
我有一个 wpf 文本块,如下所示:
I have a wpf textblock as below:
<TextBlock Text="{Binding [someViewModel].SomeVar.SomeSubVar.Name,
TargetNullValue='-'}"/>
在我的视图模型方面,我将有自己的逻辑,最终 SomeVar.SomeSubVar
将为空.
At my viewmodel side, I'll have my own logic that in the end, SomeVar.SomeSubVar
will be null.
如果我想显示此 TextBlock
的默认值,我知道我可以声明并启动 SomeVar.SomeSubVar
并将默认值分配给 SomeVar.SomeSubVar.Name
但我想改用 TargetNullValue
.我可以知道哪个部分有问题吗?
If I want to show a default value for this TextBlock
I know I can declare and initiate SomeVar.SomeSubVar
and assign default value into SomeVar.SomeSubVar.Name
but I would like to use TargetNullValue
instead. May I know which part is wrong?
推荐答案
您可能会考虑使用 FallbackValue http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue(v=vs.110)).aspx 与 TargetNullValue 结合使用.
You might look at using FallbackValue http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.fallbackvalue(v=vs.110).aspx in conjunction with TargetNullValue.
为了使 TargetNullValue 起作用,您必须能够将 [someViewModel].SomeVar.SomeSubVar.Name 的完整路径评估为 null.如果someViewModel"、SomeVar"或SomeSubVar"为空,则永远无法评估Name",并且 TargetNullValue 将不适用.
In order for TargetNullValue to work you must be able to evaluate the full path of [someViewModel].SomeVar.SomeSubVar.Name to null. If 'someViewModel', 'SomeVar', or 'SomeSubVar' are null then 'Name' can never be evaluated, and TargetNullValue won't apply.
这篇关于WPF 文本块 TargetNullValue 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!