How to bind a CommandParameter in a ContentView to an element in the parent ContentPage in Xamarin Forms

后端 未结 1 378
别那么骄傲
别那么骄傲 2021-01-17 04:32

I have a parent ContentPage with several ContentViews in separate files. I am trying to pass as reference an element in the parent ContentView to a CommandParameter in one

相关标签:
1条回答
  • 2021-01-17 04:57

    You could check the following code

    in AddressChipView.xaml

    Since you had used Custom ContentView , you need use bindable property to binding value between the elements in ContentView and Parent ContentPage

    <?xml version="1.0" encoding="UTF-8" ?>
    <ContentView
    x:Class="Acies.NitroT.Views.Home.AddressChipView"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:behaviors="clr-namespace:Acies.NitroT.Behaviors"
    xmlns:buttons="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="clr-namespace:Acies.NitroT.Controls;assembly=Acies.NitroT.Mobile.Shared"
    mc:Ignorable="d"
    
    x:Name="ChipView" // set the name of AddressChipView
    
    >
    
    <buttons:SfChipGroup
                    ChipPadding="2,0,0,0"
                    Command="{Binding Source={x:Reference ChipView}, Path=SelectedAddressCommand}"
                    //...
                    SelectedItem="{Binding Source={x:Reference ChipView}, Path=SelectedAddress}"
                    ShowIcon="True"
                    Type="Choice">
                    <buttons:SfChipGroup.Behaviors>
                        <behaviors:EventToCommandBehavior
                            Command="{Binding Source={x:Reference ChipView}, Path=AddressSelectionChangedCommand}"
                            CommandParameter="{Binding Source={x:Reference ChipView}, Path=CurrentMapView}"
                            EventName="SelectionChanged" />
                    </buttons:SfChipGroup.Behaviors>
                </buttons:SfChipGroup>
    

    in AddressChipView.xaml.cs

    public static readonly BindableProperty AddressSelectionChangedCommandProperty =
                BindableProperty.Create(nameof(AddressSelectionChangedCommand), typeof(ICommand), typeof(AddressChipView));
    
            public ICommand AddressSelectionChangedCommand
            {
                get => (ICommand)GetValue(AddressSelectionChangedCommandProperty );
                set => SetValue(AddressSelectionChangedCommandProperty , value);
            }
    
            public static BindableProperty CurrentMapViewProperty =
                BindableProperty.Create(nameof(CurrentMapView), typeof(object), typeof(AddressChipView));
    
            public object CurrentMapView
            {
                get => (object)GetValue(CurrentMapViewProperty );
                set => SetValue(CurrentMapViewProperty , value);
            }
    
            //... other bindable property  
           
    

    Now you can binding it in ContentPage

    <homeViews:AddressChipView Grid.Row="1"  AddressSelectionChangedCommand={Binding xxxCommand}  CurrentMapView = {Binding Source={x:Reference Map}, Path=.} />
    
    0 讨论(0)
提交回复
热议问题