在代码隐藏中多次绑定到 TextBlock - 我在哪里弄错了



情况很简单,我在代码中对 TextBlock 控件进行了多重绑定,如下所示:

TextBlock tb = new TextBlock();
tb.VerticalAlignment = VerticalAlignment.Center;
tb.HorizontalAlignment = HorizontalAlignment.Center;
MultiBinding multiTbBinding = new MultiBinding();
multiTbBinding.StringFormat = "{}{0} + {1}";
multiTbBinding.Bindings.Add(new Binding()
{
    Path = new PropertyPath("PraceLamania[" + vm.PraceLamania.IndexOf(dpl) + "].DPL_Temperatura"),
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
    Mode = BindingMode.OneWay,
    Source = vm
});
multiTbBinding.Bindings.Add(new Binding()
{
    Path = new PropertyPath("PraceLamania[" + vm.PraceLamania.IndexOf(dpl) + "].DPL_Wynik"),
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
    Mode = BindingMode.OneWay,
    Source = vm
});
BindingOperations.SetBinding(tb, TextBlock.TextProperty, multiTbBinding);

当我执行单个值绑定时,一切正常,但在这种情况下,我不知道我在哪里犯了错误。

我可以要求任何建议吗?

错误在StringFormat中很简单。尝试一下:

multiTbBinding.StringFormat = "{0} + {1}";

如果添加到原始代码中:

multiTbBinding.FallbackValue = "Fallback";

然后,您将在Text属性中获得回退

相关内容

  • 没有找到相关文章

最新更新