WPF多绑定 - UnsetValue问题

9

我有一个TextBlock。当它的Text被绑定为:

<Binding Path="Applicant2.Surname"/>

它正常工作,但我想包括名字,所以将绑定更改为:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Applicant2.Forenames"/>
    <Binding Path="Applicant2.Surname"/>
</MultiBinding>

这会显示 {DependencyProperty.UnsetValue} {DependencyProperty.UnsetValue} 直到第一次设置值。

我该如何避免这个问题?为什么第一个简单的绑定没有出现这个问题?

2个回答

14

对于多重绑定,如果需要添加回退值,那么如果它只是空白的,那么您可以简单地执行以下操作:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Applicant2.Forenames" FallbackValue=""/>
    <Binding Path="Applicant2.Surname" FallbackValue=""/>
</MultiBinding>

谢谢,你帮我省了很多时间。我已经花了很多时间搜索。 - David Ward

0

对于多绑定,我使用了以下代码,它能正常工作:

<MultiBinding Converter="{StaticResource ValueToAngle}" StringFormat="{}{0} {1}">
                        <MultiBinding.Bindings>
                            <Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}" Path="TotalSkidCount"/>
                            <Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}" Path="ActualCount"/>
                        </MultiBinding.Bindings>
                    </MultiBinding>

以下是它的属性:
public int ActualCount { get { return (int)GetValue(ActualCountProperty); } set { SetValue(ActualCountProperty, value); } }
public static readonly DependencyProperty ActualCountProperty = DependencyProperty.Register("ActualCount", typeof(int), typeof(CirculerProgressBarControl));

public int TotalSkidCount { get { return (int)GetValue(TotalSkidCountProperty); } set { SetValue(TotalSkidCountProperty, value); } }
public static readonly DependencyProperty TotalSkidCountProperty = DependencyProperty.Register("TotalSkidCount", typeof(int), typeof(CirculerProgressBarControl));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接