Trouble binding command to button in WPF
I've got a button in a ribbon I've implemented and having trouble binding to a command. The ribbon is in a UserControl named AppRibbon. The AppRibon control has a public property called SelectedModule which has a property named RenameModuleCmd. When I create an event handler开发者_如何学Python for the button, i call the command explicitly to make sure everything is working as shown below:
public partial class ApplicationRibbon : UserControl {
public ApplicationRibbon() {
InitializeComponent();
}
public ModuleViewModel SelectedModule { get; set; }
private void ButtonTool_Click(object sender, RoutedEventArgs e) {
SelectedModule.RenameModuleCmd.Execute(null);
}
}
This works fine. But I obviously don't want to use the code behind model... much rather use the Command binding. So I tried the folling bindings, but nothing fired the command.
<... Command="{Binding Path=SelectedModule.RenameModuleCmd}" />
<... Command="{Binding Path=AppRibbon.SelectedModule.RenameModuleCmd}" />
I can't figure out why these won't work. I've set breakpoints on the command's CanExecute and Execute methods as well on the RenameModuleCmd property's getter, but none are hit. Ideas?
You need to specify what element you're binding to when developing a UserControl and binding inside it. Try:
<... Command="{Binding ElementName=AppRibbon, Path=SelectedModule.RenameModuleCmd}" />
Assuming your UserControl is named AppRibbon as your post seems to imply.
精彩评论