My control has a property that returns an ICommand object. Using XAML, how to I bind a button to it?
+2
A:
Without knowing anything about the relation of the two I would use element binding. E.g.
<YourControl x:Name="CmdSrc" />
<Button Command={Binding ElementName=CmdSrc, Path=CmdProperty} />
You should think about the approach that a control provides a command. It's seems to me some kind of weird. ;)
Regards
EDIT
Ah ok, it was just a hint. Just in case you didn't think about it.
Here is another way to bind your command. I've to admit that I didn't test it. But I think the following should work too.
When the Button
is in the control you could also use relative binding.
<YourControl>
<Button Command={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourControl}}, Path=CmdProperty} />
</YourControl>
Then you don't need a name for the control. I'm avoiding names when ever I can to prevent dirty workarounds in code behind.
Yes, I know it some kind of paranoid. ;)
DHN
2010-07-27 06:08:14
The button is on the control that has the command. The sole purpose is to affect UI elements, so the normal Model or ViewModel patterns don't fit.
Jonathan Allen
2010-07-27 06:57:38
How about that, I didn't expect to be able to tag the top-level UserControl element with an x:Name. But it worked like a charm.
Jonathan Allen
2010-07-27 06:58:28
I edited my post.
DHN
2010-07-27 08:26:20
Interesting. I do believe your edit will also fix some other problems I've been dealing with.
Jonathan Allen
2010-08-02 21:59:22