Binding comand for Button












1














I have carousel view and bindings properties:



<ContentView>
<controls:CarouselViewControl x:Name="CarouselData" ItemsSource="{Binding StartDisplay}"
ShowIndicators="True"
ShowArrows="False"
IndicatorsTintColor="BurlyWood" CurrentPageIndicatorTintColor="DarkGoldenrod">
<controls:CarouselViewControl.ItemTemplate>
<DataTemplate >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>

<Image Grid.RowSpan="3" Aspect="AspectFill" Source="{Binding Image}"/>

<ContentView Grid.Row="0" Padding="60,30,60,0">
</ContentView>

<ContentView Grid.Row="1" Padding="20,10,20,50">
<Label Text="{Binding Message}"
TextColor="Black"
FontSize="20"
HorizontalTextAlignment="Center" />
</ContentView>

<ContentView Grid.Row="2" Padding="20,10,20,50">
<Button Text="Getted start" IsVisible="{Binding VisibleStartButton}"/>
</ContentView>
</Grid>
</DataTemplate>
</controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>
</ContentView>


I pointed ItemsSource for CarouselView.
Class ModelView:



public class StartPageViewModel: BaseViewModel // ViewModel
{
private ObservableCollection<DataCarouselView> startDisplay;
public ObservableCollection<DataCarouselView> StartDisplay
{
get => startDisplay; set => SetProperty(ref startDisplay, value);
}

public StartPageViewModel()
{
StartDisplay = new ObservableCollection<DataCarouselView>(new
{
new DataCarouselView("back_1.png", "test1"),
new DataCarouselView("back_2.png", "test2"),
new DataCarouselView("back_3.png", "test3"),
new DataCarouselView("back_4.png", "test4", true)
});
OpenMenuPageCommand = new OpenMenuPageCommand(this);
}

}


class Model:



public class DataCarouselView // Model
{
public string Image { get; set; }
public string Message { get; set; }
public bool VisibleStartButton { get; set; }

public DataCarouselView(string image, string message, bool vis = false)
{
Image = image;
Message = message;
VisibleStartButton = vis;
}
}


Everything is working.



I want to add command for to the button. I create command class:



public class OpenMenuPageCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private StartPageViewModel viewModel;
public OpenMenuPageCommand(StartPageViewModel vm)
{
viewModel = vm;
}

public bool CanExecute(object parameter)
{
return viewModel != null;
}

public void Execute(object parameter)
{
if (CanExecute(parameter))
viewModel.OpenMenuPage();
}
}


and property in class ViewModel:



public ICommand OpenMenuPageCommand { get; }


and method command in class ViewModel:



public async void OpenMenuPage()
{
await navigation.PushAsync(new MenuPage());
}


How binding command for button? Thanks.










share|improve this question



























    1














    I have carousel view and bindings properties:



    <ContentView>
    <controls:CarouselViewControl x:Name="CarouselData" ItemsSource="{Binding StartDisplay}"
    ShowIndicators="True"
    ShowArrows="False"
    IndicatorsTintColor="BurlyWood" CurrentPageIndicatorTintColor="DarkGoldenrod">
    <controls:CarouselViewControl.ItemTemplate>
    <DataTemplate >
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    </Grid.RowDefinitions>

    <Image Grid.RowSpan="3" Aspect="AspectFill" Source="{Binding Image}"/>

    <ContentView Grid.Row="0" Padding="60,30,60,0">
    </ContentView>

    <ContentView Grid.Row="1" Padding="20,10,20,50">
    <Label Text="{Binding Message}"
    TextColor="Black"
    FontSize="20"
    HorizontalTextAlignment="Center" />
    </ContentView>

    <ContentView Grid.Row="2" Padding="20,10,20,50">
    <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}"/>
    </ContentView>
    </Grid>
    </DataTemplate>
    </controls:CarouselViewControl.ItemTemplate>
    </controls:CarouselViewControl>
    </ContentView>


    I pointed ItemsSource for CarouselView.
    Class ModelView:



    public class StartPageViewModel: BaseViewModel // ViewModel
    {
    private ObservableCollection<DataCarouselView> startDisplay;
    public ObservableCollection<DataCarouselView> StartDisplay
    {
    get => startDisplay; set => SetProperty(ref startDisplay, value);
    }

    public StartPageViewModel()
    {
    StartDisplay = new ObservableCollection<DataCarouselView>(new
    {
    new DataCarouselView("back_1.png", "test1"),
    new DataCarouselView("back_2.png", "test2"),
    new DataCarouselView("back_3.png", "test3"),
    new DataCarouselView("back_4.png", "test4", true)
    });
    OpenMenuPageCommand = new OpenMenuPageCommand(this);
    }

    }


    class Model:



    public class DataCarouselView // Model
    {
    public string Image { get; set; }
    public string Message { get; set; }
    public bool VisibleStartButton { get; set; }

    public DataCarouselView(string image, string message, bool vis = false)
    {
    Image = image;
    Message = message;
    VisibleStartButton = vis;
    }
    }


    Everything is working.



    I want to add command for to the button. I create command class:



    public class OpenMenuPageCommand : ICommand
    {
    public event EventHandler CanExecuteChanged;
    private StartPageViewModel viewModel;
    public OpenMenuPageCommand(StartPageViewModel vm)
    {
    viewModel = vm;
    }

    public bool CanExecute(object parameter)
    {
    return viewModel != null;
    }

    public void Execute(object parameter)
    {
    if (CanExecute(parameter))
    viewModel.OpenMenuPage();
    }
    }


    and property in class ViewModel:



    public ICommand OpenMenuPageCommand { get; }


    and method command in class ViewModel:



    public async void OpenMenuPage()
    {
    await navigation.PushAsync(new MenuPage());
    }


    How binding command for button? Thanks.










    share|improve this question

























      1












      1








      1







      I have carousel view and bindings properties:



      <ContentView>
      <controls:CarouselViewControl x:Name="CarouselData" ItemsSource="{Binding StartDisplay}"
      ShowIndicators="True"
      ShowArrows="False"
      IndicatorsTintColor="BurlyWood" CurrentPageIndicatorTintColor="DarkGoldenrod">
      <controls:CarouselViewControl.ItemTemplate>
      <DataTemplate >
      <Grid>
      <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="100" />
      <RowDefinition Height="100" />
      </Grid.RowDefinitions>

      <Image Grid.RowSpan="3" Aspect="AspectFill" Source="{Binding Image}"/>

      <ContentView Grid.Row="0" Padding="60,30,60,0">
      </ContentView>

      <ContentView Grid.Row="1" Padding="20,10,20,50">
      <Label Text="{Binding Message}"
      TextColor="Black"
      FontSize="20"
      HorizontalTextAlignment="Center" />
      </ContentView>

      <ContentView Grid.Row="2" Padding="20,10,20,50">
      <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}"/>
      </ContentView>
      </Grid>
      </DataTemplate>
      </controls:CarouselViewControl.ItemTemplate>
      </controls:CarouselViewControl>
      </ContentView>


      I pointed ItemsSource for CarouselView.
      Class ModelView:



      public class StartPageViewModel: BaseViewModel // ViewModel
      {
      private ObservableCollection<DataCarouselView> startDisplay;
      public ObservableCollection<DataCarouselView> StartDisplay
      {
      get => startDisplay; set => SetProperty(ref startDisplay, value);
      }

      public StartPageViewModel()
      {
      StartDisplay = new ObservableCollection<DataCarouselView>(new
      {
      new DataCarouselView("back_1.png", "test1"),
      new DataCarouselView("back_2.png", "test2"),
      new DataCarouselView("back_3.png", "test3"),
      new DataCarouselView("back_4.png", "test4", true)
      });
      OpenMenuPageCommand = new OpenMenuPageCommand(this);
      }

      }


      class Model:



      public class DataCarouselView // Model
      {
      public string Image { get; set; }
      public string Message { get; set; }
      public bool VisibleStartButton { get; set; }

      public DataCarouselView(string image, string message, bool vis = false)
      {
      Image = image;
      Message = message;
      VisibleStartButton = vis;
      }
      }


      Everything is working.



      I want to add command for to the button. I create command class:



      public class OpenMenuPageCommand : ICommand
      {
      public event EventHandler CanExecuteChanged;
      private StartPageViewModel viewModel;
      public OpenMenuPageCommand(StartPageViewModel vm)
      {
      viewModel = vm;
      }

      public bool CanExecute(object parameter)
      {
      return viewModel != null;
      }

      public void Execute(object parameter)
      {
      if (CanExecute(parameter))
      viewModel.OpenMenuPage();
      }
      }


      and property in class ViewModel:



      public ICommand OpenMenuPageCommand { get; }


      and method command in class ViewModel:



      public async void OpenMenuPage()
      {
      await navigation.PushAsync(new MenuPage());
      }


      How binding command for button? Thanks.










      share|improve this question













      I have carousel view and bindings properties:



      <ContentView>
      <controls:CarouselViewControl x:Name="CarouselData" ItemsSource="{Binding StartDisplay}"
      ShowIndicators="True"
      ShowArrows="False"
      IndicatorsTintColor="BurlyWood" CurrentPageIndicatorTintColor="DarkGoldenrod">
      <controls:CarouselViewControl.ItemTemplate>
      <DataTemplate >
      <Grid>
      <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="100" />
      <RowDefinition Height="100" />
      </Grid.RowDefinitions>

      <Image Grid.RowSpan="3" Aspect="AspectFill" Source="{Binding Image}"/>

      <ContentView Grid.Row="0" Padding="60,30,60,0">
      </ContentView>

      <ContentView Grid.Row="1" Padding="20,10,20,50">
      <Label Text="{Binding Message}"
      TextColor="Black"
      FontSize="20"
      HorizontalTextAlignment="Center" />
      </ContentView>

      <ContentView Grid.Row="2" Padding="20,10,20,50">
      <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}"/>
      </ContentView>
      </Grid>
      </DataTemplate>
      </controls:CarouselViewControl.ItemTemplate>
      </controls:CarouselViewControl>
      </ContentView>


      I pointed ItemsSource for CarouselView.
      Class ModelView:



      public class StartPageViewModel: BaseViewModel // ViewModel
      {
      private ObservableCollection<DataCarouselView> startDisplay;
      public ObservableCollection<DataCarouselView> StartDisplay
      {
      get => startDisplay; set => SetProperty(ref startDisplay, value);
      }

      public StartPageViewModel()
      {
      StartDisplay = new ObservableCollection<DataCarouselView>(new
      {
      new DataCarouselView("back_1.png", "test1"),
      new DataCarouselView("back_2.png", "test2"),
      new DataCarouselView("back_3.png", "test3"),
      new DataCarouselView("back_4.png", "test4", true)
      });
      OpenMenuPageCommand = new OpenMenuPageCommand(this);
      }

      }


      class Model:



      public class DataCarouselView // Model
      {
      public string Image { get; set; }
      public string Message { get; set; }
      public bool VisibleStartButton { get; set; }

      public DataCarouselView(string image, string message, bool vis = false)
      {
      Image = image;
      Message = message;
      VisibleStartButton = vis;
      }
      }


      Everything is working.



      I want to add command for to the button. I create command class:



      public class OpenMenuPageCommand : ICommand
      {
      public event EventHandler CanExecuteChanged;
      private StartPageViewModel viewModel;
      public OpenMenuPageCommand(StartPageViewModel vm)
      {
      viewModel = vm;
      }

      public bool CanExecute(object parameter)
      {
      return viewModel != null;
      }

      public void Execute(object parameter)
      {
      if (CanExecute(parameter))
      viewModel.OpenMenuPage();
      }
      }


      and property in class ViewModel:



      public ICommand OpenMenuPageCommand { get; }


      and method command in class ViewModel:



      public async void OpenMenuPage()
      {
      await navigation.PushAsync(new MenuPage());
      }


      How binding command for button? Thanks.







      c# xaml xamarin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 22:23









      Range

      1126




      1126
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You need to use relative source binding to make it work



          First set the name of your root control as below:



          <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Name="InfoView"
          x:Class="InfoSeries.Views.InfoView">


          Then you can reference InfoView in order to bind Button Command with your ViewModel:



          <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}" Command="{Binding Source={x:Reference InfoView}, Path=BindingContext.OpenMenuPageCommand}"/>





          share|improve this answer





















          • Thank you so much! It worked :)
            – Range
            Nov 23 at 10:37











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438616%2fbinding-comand-for-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You need to use relative source binding to make it work



          First set the name of your root control as below:



          <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Name="InfoView"
          x:Class="InfoSeries.Views.InfoView">


          Then you can reference InfoView in order to bind Button Command with your ViewModel:



          <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}" Command="{Binding Source={x:Reference InfoView}, Path=BindingContext.OpenMenuPageCommand}"/>





          share|improve this answer





















          • Thank you so much! It worked :)
            – Range
            Nov 23 at 10:37
















          2














          You need to use relative source binding to make it work



          First set the name of your root control as below:



          <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Name="InfoView"
          x:Class="InfoSeries.Views.InfoView">


          Then you can reference InfoView in order to bind Button Command with your ViewModel:



          <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}" Command="{Binding Source={x:Reference InfoView}, Path=BindingContext.OpenMenuPageCommand}"/>





          share|improve this answer





















          • Thank you so much! It worked :)
            – Range
            Nov 23 at 10:37














          2












          2








          2






          You need to use relative source binding to make it work



          First set the name of your root control as below:



          <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Name="InfoView"
          x:Class="InfoSeries.Views.InfoView">


          Then you can reference InfoView in order to bind Button Command with your ViewModel:



          <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}" Command="{Binding Source={x:Reference InfoView}, Path=BindingContext.OpenMenuPageCommand}"/>





          share|improve this answer












          You need to use relative source binding to make it work



          First set the name of your root control as below:



          <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Name="InfoView"
          x:Class="InfoSeries.Views.InfoView">


          Then you can reference InfoView in order to bind Button Command with your ViewModel:



          <Button Text="Getted start" IsVisible="{Binding VisibleStartButton}" Command="{Binding Source={x:Reference InfoView}, Path=BindingContext.OpenMenuPageCommand}"/>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 1:55









          Dishant

          872512




          872512












          • Thank you so much! It worked :)
            – Range
            Nov 23 at 10:37


















          • Thank you so much! It worked :)
            – Range
            Nov 23 at 10:37
















          Thank you so much! It worked :)
          – Range
          Nov 23 at 10:37




          Thank you so much! It worked :)
          – Range
          Nov 23 at 10:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438616%2fbinding-comand-for-button%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)