Xamarin Forms Databinding “.” separator

前端 未结 2 421
野的像风
野的像风 2020-12-16 00:39

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

IsVisible=\"{Binding Path=UserCont         


        
相关标签:
2条回答
  • 2020-12-16 00:42

    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

    0 讨论(0)
  • 2020-12-16 01:05

    I'm assuming you're trying in Xaml. Try removing 'Path'.

    IsVisible="{Binding UserContext.IsLoggedOut}"
    

    However more importantly, what is your BindingContext? For the above code to work you would need the BindingContext to be set to class Foo, which has a property called UserContext, which itself has a property IsLoggedOut.

    Have a look here as well

    0 讨论(0)
提交回复
热议问题