简体   繁体   English

如何将ObservableCollection正确绑定到Custom Control属性?

[英]How to bind properly and ObservableCollection to a Custom Control property?

I have created a Custom Control with some properties to bind, in my case just to make visible or not the control itself and the other one to pass a previous populated ObservableCollection to create the control content. 我已经创建了一个自定义控件,该控件具有一些要绑定的属性,在我的情况下,该控件只是使控件本身可见或不可见,而另一个控件则传递先前填充的ObservableCollection来创建控件内容。

First one works perfectly but the ObservableCollection is not being correctly bound even if I added it in the XAML. 第一个可以正常运行,但是即使我在XAML中添加了ObservableCollection,也无法正确绑定它。

This is what I got when I try to iterate it inside queue control after making it visible: 这是我在使其可见后尝试在队列控件中进行迭代时得到的结果:

https://i.imgur.com/dddFoWu.png https://i.imgur.com/dddFoWu.png

What I'm doing wrong? 我做错了什么?

Thanks in advance. 提前致谢。

This is my property: 这是我的财产:

public static readonly BindableProperty ContractsListProperty = BindableProperty.Create(nameof(ContractList), typeof(ObservableCollection<object>), typeof(PrivacyControl), new ObservableCollection<object>(), BindingMode.TwoWay, propertyChanged: ContractListPropertyChangedDelegate);
public ObservableCollection<object> ContractList 
{
            get => 
(ObservableCollection<object>)GetValue(ContractsListProperty);
            set => SetValue(ContractsListProperty, value);
}

This is my control in XAML: 这是我在XAML中的控件:

<controls:PrivacyControl IsPrivacyVisible="{Binding IsPrivacyStackVisible}" ContractList="{Binding CardsAndLoansList}" />

EDITED: 编辑:

This is my VM where I check and add Cards/Loans to the ObservableCollection<object> , "CardsAndLoans" is also initialized in the ctor. 这是我的VM,我在其中检查Cards / Loans并将Cards / Loans添加到ObservableCollection<object> ,“ CardsAndLoans”也在ctor中初始化。

private ObservableCollection<object> cardsAndLoansList;
public ObservableCollection<object> CardsAndLoansList
{
get => cardsAndLoansList;
set { cardsAndLoansList = value; RaisePropertyChanged(); }
}

private async Task InitUserInfoAndPrivacy()
{
            CardsAndLoansList.Clear();

            await InitUserInformation();

            var hasLoans = GlobalSettings.Loans.NotNullOrEmpty();
            if (hasLoans)
            {
                foreach (var loan in GlobalSettings.Loans)
                    CardsAndLoansList.Add(loan);
            }

            ExampleList = CardsAndLoansList.ToList(); //<=== This is from an another try with List<obj>. 

            IsPrivacyStackVisible = UserData.ContractList.NotNullOrEmpty() || hasLoans;
}

There seems to be a typo. 似乎有错字。 The BindableProperty is called ContractsListProperty and the property itself is called ContractList (missing an s). BindableProperty称为ContractsListProperty ,而属性本身称为ContractList (缺少s)。 Change this to ContractsList and it should work (the BindableProperty name has to be the name of the property + "Property") 将此更改为ContractsList ,它应该可以工作( BindableProperty名称必须是属性名称+“ Property”)

Hope this helps 希望这可以帮助

You need to give a name to your custom control and then add the source reference to the binding properties in the Xaml. 您需要给自定义控件命名,然后将源引用添加到Xaml中的绑定属性。 For example, 例如,

top xaml tag should have name, 顶级xaml标签应具有名称,

<ContentView
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core" xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Sample.Controls.List"
         x:Name="listControl">

and then when binding the property, add the source reference like below, 然后在绑定属性时,如下添加源引用,

<ListView  IsVisible="{Binding IsPrivacyVisible, Source={x:Reference listControl}}" ItemSource="{Binding ContractList, Source={x:Reference listControl}}" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ComboBox绑定到ObservableCollection属性? - How to bind ComboBox to ObservableCollection property? 将属性绑定到自定义控件 - Bind a property to a custom control 如何在自定义控件的视图模型中绑定依赖项属性 - How to bind a Dependency Property in a Viewmodel of a Custom Control 如何将ItemsSource绑定到父窗口的ObservableCollection属性? - How to bind ItemsSource to ObservableCollection Property of the parent Window? 如何使用附加属性绑定XyDataSeries的ObservableCollection - How to Bind an ObservableCollection of XyDataSeries using an Attached Property .NET MAUI - 如何绑定 ObservableCollection 项的属性 - .NET MAUI - How to bind on a property of an ObservableCollection Item 如何将ObservableCollection绑定到XAML中的IList属性? - How to bind ObservableCollection to IList property in XAML? WPF自定义控件-如何从静态ObservableCollection绑定列表框? - WPF Custom Control - How do you bind a listbox from a static ObservableCollection? 如何将自定义控件的 ObservableCollection 绑定到 StackPanel? - How to bind a ObservableCollection of custom controls to a StackPanel? 如何将自定义控件的依赖项属性绑定到其视图模型属性 - How to bind a dependency property for a custom control to its view models property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM