data-binding - Xamarin 表单数据绑定(bind) "."分隔符

标签 data-binding xamarin.forms

我正在努力处理 Xamarin Forms 中的数据绑定(bind)。这就是我期望从以下 XAML 语句中发生的情况:

IsVisible="{Binding Path=UserContext.IsLoggedOut}"

是 - 该属性绑定(bind)到 View 模型的子对象。现在,要么 Xamarin Forms 不支持此功能,要么我错过了一个技巧。

如果 Xamarin 不支持这一点,而 WPF 则支持这一点,那么我们应该怎么做才能传播嵌套对象?扁平化 View 模型让我编写了大量不优雅的代码。

最佳答案

支持嵌套属性,就像其他相当复杂的表达式一样:

你可以测试一下:

Xaml

<StackLayout Spacing="20">
  <StackLayout Orientation="Horizontal">
    <Label Text="Subproperties: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.SubItem.Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Dictionary[key].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
  <StackLayout Orientation="Horizontal">
    <Label Text="Array Indexer: " HorizontalOptions="FillAndExpand" FontSize="15"></Label>
    <Label Text="{Binding Item.Array[1].Text}" HorizontalOptions="FillAndExpand" FontSize="15"></Label>
  </StackLayout>
</StackLayout>

页面

public partial class Page2 : ContentPage
{
    public ItemModel Item { get; }

    public Page2()
    {
        InitializeComponent();
        Item = new ItemModel();
        BindingContext = this;

    }
}

public class ItemModel
{
    public ItemSubModel SubItem { get; set; }
    public Dictionary<string, ItemSubModel>  Dictionary { get; set; }
    public ItemSubModel[] Array { get; set; }

    public ItemModel()
    {
        SubItem = new ItemSubModel();
        Dictionary = new Dictionary<string, ItemSubModel>
        {
            {"key", new ItemSubModel()}
        };
        Array = new [] {new ItemSubModel(), new ItemSubModel() };
    }
}

public class ItemSubModel
{
    public string Text { get; set; } = "Supported";
}

结果

enter image description here

关于data-binding - Xamarin 表单数据绑定(bind) "."分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985634/

相关文章:

ASP.NET图表控件多系列数据绑定(bind)

mvvm - 如何将多个 View 模型绑定(bind)到单个 View

android - 如何从我的 C# 代码中通过自定义 url 方案访问传递到我的应用程序中的值?

Xamarin Forms 可绑定(bind)属性与 OneWay 不起作用

.net - 使用 WPF 双向数据绑定(bind)时更改 setter 属性中的值

c++ - Q_PROPERTY NOTIFY 信号及其参数

html - Paypal 立即购买按钮 - 通过下拉菜单动态价格

xamarin - 如何在Xamarin中的XAML中设置选项卡式页面的内容页面?

c# - Xamarin 形成自定义单元格货币格式?

windows-phone-7 - Windows Phone 7 数据绑定(bind)外部属性(非静态超链接文本)