【问题标题】:Set equivalent of {Binding .} from code behind in Xamain?从 Xamarin 后面的代码中设置等效的 {Binding .}?
【发布时间】:2020-07-17 10:12:07
【问题描述】:

我目前正在从后面的代码中设置标签绑定,如下所示:

lblLabel.SetBinding(Label.TextProperty, model.DataField);

但是我现在需要向标签添加多个绑定(model.Title 和 model.DataField),因此我想绑定整个 BindingContext 以在转换器中使用,在 XAML 中我会使用:

{Binding .}

所以从后面的代码中尝试了以下,但这只是将 null 传递给转换器?

lblLabel.SetBinding(Label.TextProperty, ".", BindingMode.Default, new ModelToStringConverter());

这可以从后面的代码中实现还是只能使用“。”直接在 XAML 中?

【问题讨论】:

  • 为什么不在 ViewModel 中结合 model.Title 和 model.DataField,然后用这个值绑定到一个新的属性。那么你就不需要使用转换器了。

标签: c# xaml xamarin xamarin.forms model-binding


【解决方案1】:

XAML 的"." 等价于this

对于您的场景,您需要将路径作为字符串 tho,因此您可以通过创建新的 Binding 来使用 "."

ModelToStringConverter modelToStringConverter = new ModelToStringConverter();
txtBlock.SetBinding(TextBlock.TextProperty, new Binding(".", converter: modelToStringConverter);

【讨论】:

  • 感谢您的回答,我试过了,但收到错误“Argument type '...' is not assignable to parameter type 'string'”?
  • 我现在看到了您的问题。 this 是适当的等价物,但由于它不是 string,所以不能在这里使用。我将测试并尝试找到解决方案。
  • @Apqu 请查看我更新的答案并检查这​​是否有效。
猜你喜欢
  • 2018-07-03
  • 2012-03-30
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多