This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have defined the following split button in a DataTemplate, but I am having a horrible time getting the command binding to work on the MenuItems. I'm sure it is something stupid, but for the life of me I can't see it. The Command binding on the actual split button works perfectly, but when I do the same binding on the MenuItems I get a binding error. The command I am trying to find in the first menu item is the same exact command that is on the split button. My first thought was that this was a similar problem to context menus - because they are not in the visual tree. But that doesn't seem to be an issue because using Snoop I can see the MenuItems as direct descendants to the UserControl. I have tried various other bindings, such as using ElementName and such, but I have had no success so far. As I mentioned before, the fact that the SplitButton is in a DataTemplate might have something to do with this. In case you are wondering the datatemplate is the NodeTemplate for a DiagramNodeElement. I don't know if that's relevant, but I figured I had better mention it.
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=AddCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') |
|
|
Hi eliz, The menu should be in the adorner layer, rather than part of the same visual tree as the UserControl. That is what I'm seeing at my end anyway. That would certainly cause the bindings to fail here. In which case, I would recommend trying to make your commands statically accessible so that they can be set on each MenuItem rather than bound. To do this, you could either use the following code in your UserControl, or in a static class for all your related commands:
And use this in xaml like this:
(Where "CommandHost" should be replaced with the name of the class where you put your static commands). I have tried this and it works fine. I'm not sure about your Data bindings though. They may also not work. Depending on where your command event handlers are, maybe you'll be able to get the Data within them rather than having a command parameter. Hope that helps to work around this issue. -Jason Fauchelle |
|
|
That does the trick. Thanks. |
|