views:

115

answers:

1

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 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?

A: 

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.

JustABill
Thanks JustABill, but I was doing this within the UserControl where SelectedModule is defined. Thus I didn't think that the ElementName was relevant. I don't have the app handy to test this, but I suspect it won't make a change.
AC
That seems intuitive, but trust me, you can't bind to a property of a UserControl (or a Window, or any other 'outermost' element for that matter) unless you pass an ElementName or the DataContext is already set. If you've done the latter, then I just misread and I apologize.
JustABill
I just verified the name (<UserControl x:Name="Ribbon">) and then added this as the binding: Command="{Binding ElementName=Ribbon, Path=SelectedModule.RenameModuleCmd}"The Ribbon UserControl has a public property of SelectedModule which is of type ModuleViewModel which contains public property of type ICommand named RenameModuleCmd. I set breakpoints in the getter and unfortunately nothing happens. However in that same button if I add a click handler that does the following, I get the desired behavior (prefer to avoid events tho): SelectedModule.RenameModuleCmd.Execute(null); Ideas?
AC
I'd suggest you look at http://bea.stollnitz.com/blog/?p=52 for how to see where your binding is failing. If it doesn't show up there, I really don't know. Maybe implement INotifyPropertyChanged?
JustABill