WPF Button In a Popup does not fire and freezes control
I have a popup in a custom control that popups with a mouseenter event. The popup and button display fine. When I click on the button, the control freezes and does not fire the command. To unfreeze, I have to minimize the window.
The popup code is as follows:
<Popup Name="Popup_PrevButton"
PlacementTarget="{Binding ElementName=PART_Button}"
Placement="Left"
StaysOpen="False"
AllowsTransparency="True"
PopupAnimation="Fade"
Focusable="True">
<Grid>
<Button
Name="Button_PrevButton"
Margin="2,0,2,0"
Width="20"
Height="48"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Foreground="{TemplateBinding Foreground}"
Command="{Binding Path=Button_NextButton}">
</Button>
</Grid>
</Popup>
If I use a Click=Button_NextButton I get in an error that states: Failed to create a 'Click' from the text 'Button_NextButton'.
Update
I have updated my code and implemented the RelayCommand class as suggested. I changed the binding to
Command="{Binding Path=nCommand:CustomCommands.Button_Click}">
I am confident that i have bound the command (By changing things I break the program so it won't run - e.g.: if i remove the sub, then i get开发者_如何学C an error that the sub doesn't exist), but when i click on the button, and the same thing happens.
The button command is as follows:
Private Sub ON_Button_Click(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
'Do something here
End Sub
Code for nCommand: Public Shared Button_PrevButton_Click As New RoutedCommand("Button_PrevButton_Click", GetType(MainWindow))
Binding in MainWindow:
So basically i know the compiler is seeing things, but when i actually click it doesn't pass it to the sub. any further thoughts?
The button's Command property should be bound to something on the object set as the DataContext which implements the ICommand interface (as H.B. mentioned).
An easy way to achieve this is to use a wrapper class which contains much of the mechanics of the ICommand implementation. Here's a link to a good introductory article on MVVM containing an implementation of the RelayCommand
class which does this.
The entire article is a great read, but you can skip down to the "Relaying Command Logic" section to see the implementation.
Hope that helps!
If you get such an error the method signature you created in code was wrong, e.g. specified MouseEventArgs
instead of generic RoutedEventArgs
.
Besides that i see no problem with this bit of XAML, if the binding actually binds to a command which does not do some rather weird things it should work. The fact that the button is inside a popup should not be a problem.
精彩评论