Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在自定义控件中从CollectionView绑定ItemsSource?_C#_Xamarin.forms - Fatal编程技术网

C# 如何在自定义控件中从CollectionView绑定ItemsSource?

C# 如何在自定义控件中从CollectionView绑定ItemsSource?,c#,xamarin.forms,C#,Xamarin.forms,我正在使用CollectionView制作一个自定义控件。问题是,我想从主页而不是控件中为其提供ItemsSource,并且ItemsSource将其与我的ViewModel(我的ObservableCollection所在的位置)进行通信。这是我的代码: FloatingButton.xaml: 您可以将ItemSource设置为可绑定属性,而不是整个CollectionView 顺便说一下,因为您使用了自定义视图,所以需要直接在xaml中设置绑定路径。行BindingContext=thi

我正在使用CollectionView制作一个自定义控件。问题是,我想从主页而不是控件中为其提供ItemsSource,并且ItemsSource将其与我的ViewModel(我的ObservableCollection所在的位置)进行通信。这是我的代码:

FloatingButton.xaml:


您可以将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"        
        
        />