在代码隐藏中多次绑定到 TextBlock - 我在哪里弄错了?
Multiple binding to TextBlock in code behind - where did I mistake?
案例很简单,我在代码后面对 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
属性.
中获得 Fallback
案例很简单,我在代码后面对 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
属性.