MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.28k stars 2.45k forks source link

Command binding not working on FlipView next item #3375

Closed jfquarto closed 5 years ago

jfquarto commented 5 years ago

My FlipView in Xaml:

<Controls:FlipView ItemsSource="{Binding MyTabs}" CircularNavigation="True" SelectedIndex="0">
    <Controls:FlipView.ItemTemplate>
        <DataTemplate>
            <ContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding Buttons}" />
         </DataTemplate>
    </Controls:FlipView.ItemTemplate>
</Controls:FlipView>

MyTabs is an ObservableCollection of class Tab. Class Tab has a property Buttons which is a Grid with Button controls on it. Each button has a Command binding (MyTabs is created in the viewmodel constructor):

button.SetBinding(Button.CommandProperty, new Binding("DataContext.ButtonCommand") { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FlipView),1) });
button.CommandParameter = id.ToString();

Now all works fine on the first FlipView item selected via SelectedIndex. If I click on a button the Command is executed. But when I navigate to next item the command binding of the buttons on that item don't work. Same goes for the next items. Only the buttons on the initial selected item will work ok. For example if I change the value of SelectedIndex in the Xaml to 2, then only the buttons on item 2 work ok.

jfquarto commented 5 years ago

Do you need more information ?

jfquarto commented 5 years ago

If I use this instead of Command: button.Click += Button_Click; it works for every item I navigate to. But I want to use Command because of MVVM.

punker76 commented 5 years ago

@jfquarto Which MahApps version are you using?

punker76 commented 5 years ago

@jfquarto I think this is fixed in the current dev branch b83db30511fed40eda672c0e302abc8f92348d39

jfquarto commented 5 years ago

Build MahApps from the current dev branch and used it in my project. But it didn't solve my problem.

punker76 commented 5 years ago

@jfquarto It would be nice if you can create a short sample and separate this.

jfquarto commented 5 years ago

Here's the sample; FlipViewTest.zip

punker76 commented 5 years ago

@jfquarto The problem here is, that you are mixing MVVm with creating visuals in code behind. It works for the first tab, because there is a existing visual tree and for the other tabs it doesn't exists. In your case you can do this at the KeyboardCreator:

...

//
//the command binding
//
button.CommandParameter = id.ToString();
grid2.Children.Add(button);
grid2.Loaded += (sender, args) =>
{
    BindingOperations.ClearBinding(button,Button.CommandProperty);
    button.SetBinding(Button.CommandProperty, new Binding("DataContext.KeyCommand") {RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FlipView), 1)});
};

...
jfquarto commented 5 years ago

Thanx !

Met vriendelijke groet, ​ Johan Fontijne Software developer

Quarto B.V. Westbaan 200 Moordrecht 2841 MC Nederland Tel: +31 182 700 500 Email: johan.fontijne@quarto.nl Web: www.quarto.nl De disclaimer van toepassing op e mail van Quarto Betaalsystemen vindt u op https://www.quarto.nl/disclaimer-quarto Van: Jan Karger notifications@github.com Verzonden: Friday, 14 December 2018 17:04 Aan: MahApps/MahApps.Metro MahApps.Metro@noreply.github.com CC: Johan Fontijne johan.fontijne@quarto.nl; Mention mention@noreply.github.com Onderwerp: Re: [MahApps/MahApps.Metro] Command binding not working on FlipView next item (#3375)

@jfquartohttps://github.com/jfquarto The problem here is, that you are mixing MVVm with creating visuals in code behind. It works for the first tab, because there is a existing visual tree and for the other tabs it doesn't exists. In your case you can do this at the KeyboardCreator:

...

//

//the command binding

//

button.CommandParameter = id.ToString();

grid2.Children.Add(button);

grid2.Loaded += (sender, args) =>

{

BindingOperations.ClearBinding(button,Button.CommandProperty);

button.SetBinding(Button.CommandProperty, new Binding("DataContext.KeyCommand") {RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FlipView), 1)});

};

...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/MahApps/MahApps.Metro/issues/3375#issuecomment-447369586, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AWHPuEk1k8-Cq2XmgBxsK9ZUckGFqdApks5u48wHgaJpZM4YtTz6.