WPF MultiBinding - UnsetValue 问题

发布于 2024-09-07 14:45:50 字数 442 浏览 9 评论 0原文

我有一个文本块。当其文本绑定为:

<Binding Path="Applicant2.Surname"/>

它工作正常,但是我想包含名字,因此将绑定更改为:

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

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

我怎样才能阻止这个?为什么我在第一个简单绑定时没有遇到问题?

I have a TextBlock. When its Text is bound as:

<Binding Path="Applicant2.Surname"/>

It works fine, however I want to include the Forenames so changed the binding to:

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

This displays {DependencyProperty.UnsetValue} {DependencyProperty.UnsetValue} until the value is set the first time.

How can I stop this? Why do I not get the problem with the first simple binding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2024-09-14 14:45:50

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

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

for a multibinding you need to add a fallback value if it is just blank then you can simply do:

<MultiBinding StringFormat="{}{0} {1}">
    <Binding Path="Applicant2.Forenames" FallbackValue=""/>
    <Binding Path="Applicant2.Surname" FallbackValue=""/>
</MultiBinding>
夏夜暖风 2024-09-14 14:45:50

对于多重绑定,我使用了下面的代码并为我工作:

<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));

For multibinding, I used below code and worked for me :

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

Below is the property of it :

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文