tags:

views:

34

answers:

1

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
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
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
I edited my post.
DHN
Interesting. I do believe your edit will also fix some other problems I've been dealing with.
Jonathan Allen