C# 如何在自定义控件中从CollectionView绑定ItemsSource?
我正在使用CollectionView制作一个自定义控件。问题是,我想从主页而不是控件中为其提供ItemsSource,并且ItemsSource将其与我的ViewModel(我的ObservableCollection所在的位置)进行通信。这是我的代码: FloatingButton.xaml:C# 如何在自定义控件中从CollectionView绑定ItemsSource?,c#,xamarin.forms,C#,Xamarin.forms,我正在使用CollectionView制作一个自定义控件。问题是,我想从主页而不是控件中为其提供ItemsSource,并且ItemsSource将其与我的ViewModel(我的ObservableCollection所在的位置)进行通信。这是我的代码: FloatingButton.xaml: 您可以将ItemSource设置为可绑定属性,而不是整个CollectionView 顺便说一下,因为您使用了自定义视图,所以需要直接在xaml中设置绑定路径。行BindingContext=thi
您可以将ItemSource设置为可绑定属性,而不是整个CollectionView 顺便说一下,因为您使用了自定义视图,所以需要直接在xaml中设置绑定路径。行
BindingContext=this代码>将断开CustomView和ContentPage之间的绑定
所以您可以像下面这样修改代码
<local:FloatingButton
ItemSource="{Binging ItemList}"
CollectionViewVisible = "{Binding IsVisible}"
PrimaryImageSource="dots.png"
PrimaryButtonColor="Green"
/>
在FloatingButton.xaml中
公共浮动按钮()
{
初始化组件();
//BindingContext=这个;
}
//...
//===================项目来源=====================
公共静态只读BindableProperty ItemSourceProperty=
Create(“ItemSource”、typeof(observetecollection)、typeof(FloatingButton));
公共可观测集合项源
{
get{return(ObservableCollection)GetValue(ItemSourceProperty);}
set{SetValue(ItemSourceProperty,value);}
}
公共静态只读BindableProperty CollectionViewVisibleProperty=
Create(“PrimaryImageSource”、typeof(bool)、typeof(FloatingButton)、false);
公共布尔集合视图可见
{
获取{return(bool)GetValue(CollectionViewVisibleProperty);}
set{SetValue(CollectionViewVisibleProperty,value);}
}
公共静态只读BindableProperty LaunchWebProperty=
创建(nameof(LaunchWeb)、typeof(ICommand)、typeof(FloatingButton));
公共ICommand LaunchWeb
{
get=>(ICommand)GetValue(LaunchWebProperty);
set=>SetValue(LaunchWebProperty,value);
}
公共静态BindableProperty网站属性=
创建(nameof(网站)、typeof(对象)、typeof(浮动按钮));
公共对象网站
{
get=>(对象)GetValue(WebsiteProperty);
set=>SetValue(WebsiteProperty,value);
}
//其他像这样的可绑定属性
在ContentPage中
现在您可以按如下方式设置绑定
<local:FloatingButton
ItemSource="{Binging ItemList}"
CollectionViewVisible = "{Binding IsVisible}"
PrimaryImageSource="dots.png"
PrimaryButtonColor="Green"
/>
<local:FloatingButton
ItemSource="{Binging ItemList}"
CollectionViewVisible = "{Binding IsVisible}"
PrimaryImageSource="dots.png"
PrimaryButtonColor="Green"
/>