C# 具有命令和参数绑定的BindingContext

C# 具有命令和参数绑定的BindingContext,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我有一个ViewModel,从中我构造了一个对象集合。我希望能够在视图上绑定我的对象,同时能够使用我的按钮 目前,我的属性已正确绑定到我的标签(partnerLogo、partnerText、partnerLink)。不幸的是,我无法使用button命令发送绑定的命令参数 视图模型 Public class CarouselViewModel : INotifyPropertyChanged { public CarouselViewModel() { partn

我有一个ViewModel,从中我构造了一个对象集合。我希望能够在视图上绑定我的对象,同时能够使用我的
按钮

目前,我的属性已正确绑定到我的标签(partnerLogo、partnerText、partnerLink)。不幸的是,我无法使用button命令发送绑定的命令参数

视图模型

Public class CarouselViewModel : INotifyPropertyChanged
{
    public CarouselViewModel()
    {
        partners = new ObservableCollection<Carousel>()
        {
            new Carousel(){partnerText = "Test123", partnerLogo = "Test123", partnerLink = "Test123" },
        };

        openWebsite = new Command<string>((key) =>
        {
            Device.OpenUri(new Uri(key));
        });
    }
    public ObservableCollection<Carousel> partners
    {
        get;
        set;
    }
    public ICommand openWebsite
    {
        private set;
        get;
    }
}
Public类CarouselViewModel:INotifyPropertyChanged
{
公共旋转木马模型()
{
partners=新的ObservableCollection()
{
新建旋转木马(){partnerText=“Test123”,partnerLogo=“Test123”,partnerLink=“Test123”},
};
openWebsite=新命令((键)=>
{
OpenUri(新Uri(key));
});
}
公共收集伙伴
{
收到
设置
}
公共ICommand开放网站
{
私人设置;
收到
}
}
XAML:

<CarouselView ItemsSource="{Binding partners}">
    <CarouselView.ItemsLayout>
        <GridItemsLayout/>
             </CarouselView.ItemsLayout>
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                          <Frame>
                              <StackLayout>
                                  <Image Source="{Binding partnerLogo}"/>
                                  <Label Text="{Binding partnerText}"/>
                                  <Button Text="Read" Command="{Binding openWebsite}" CommandParameter="{Binding partnerLink}"/>
                             </StackLayout>
                         </Frame>
                     </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>


如何访问我的按钮命令,同时能够发送commandparameter?

您可以做的是,为您正在使用的内容页提供一个
x:name
:-

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyProject;assembly= MyProject"
             x:Class="MyProject.Views.MyPage"
             x:Name="ThisPage">

然后将源代码提供给该命令。这样地。此方法将调用当前页面的ViewModel中的命令。此外,您还可以使用命令参数了解哪个项触发了命令

<CarouselView ItemsSource="{Binding partners}">
    <CarouselView.ItemsLayout>
        <GridItemsLayout/>
             </CarouselView.ItemsLayout>
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                          <Frame>
                              <StackLayout>
                                  <Image Source="{Binding partnerLogo}"/>
                                  <Label Text="{Binding partnerText}"/>
                                  <Button Text="Read" Command="{Binding openWebsite, Source={x:Reference Name=ThisPage}}" CommandParameter="{Binding partnerLink}"/>
                             </StackLayout>
                         </Frame>
                     </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>

对于
旋转木马视图中的每个项目,绑定上下文将是绑定到
项目资源
的基础数据项,即
旋转木马
。对于
CarouselView
,绑定上下文将是您的
ViewModel
,如果您已经定义了它

要解决此问题,请在XAML中将x:Name=“mycaroselview”设置为
CarouselView
,并将其路径引用引用引用到按钮命令,如下面的代码示例所示

<CarouselView x:Name="MyCarousel" ItemsSource="{Binding partners}">
<CarouselView.ItemsLayout>
    <GridItemsLayout/>
         </CarouselView.ItemsLayout>
             <CarouselView.ItemTemplate>
                 <DataTemplate>
                      <Frame>
                          <StackLayout>
                              <Image Source="{Binding partnerLogo}"/>
                              <Label Text="{Binding partnerText}"/>
                              <Button Text="Read" Command="{Binding BindingContext.openWebsite, Source={x:Reference Name=MyCarousel}}" CommandParameter="{Binding partnerLink}"/>
                         </StackLayout>
                     </Frame>
                 </DataTemplate>
        </CarouselView.ItemTemplate>

确保必须将绑定上下文设置为
ContentPage
CarouselView

问候,
Dinesh

可能重复的