Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

data binding - Xamarin Forms Databinding "." separator

I'm struggling with databinding in Xamarin Forms. Here's why, what I expect to happen from the following XAML statement:

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

Is - That the property is bound to a child object of the View Model. Now either this isn't supported in Xamarin Forms or I am missing a trick.

If this is not supported in Xamarin, where it is supported in WPF, then what are we supposed to do in order to propagate nested objects? Flattening the View Model is making me write reams and reams of inelegant code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nested properties are supported, like other pretty complex expression as well:

You can test it:

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>

Page

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";
}

Result

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...