views:

56

answers:

0

Please excuse me if this is a dumb question - I have read a dozen articles on the WPF command infrastructure and I am more confused than ever.

I understand that I can use the built-in commands to implement (for example) a toolbar that will apply standard cut/copy/paste functionality to any focused textbox:

<ToolBar>
    <Button Command="{x:Static ApplicationCommands.Cut}">Cut</Button>
    <Button Command="{x:Static ApplicationCommands.Copy}">Copy</Button>
    <Button Command="{x:Static ApplicationCommands.Paste}">Paste</Button>
</ToolBar>

But I want to extend this pattern so that a single toolbar can be used to invoke similar (but not identical) operations on a range of different controls. For example, I might create a custom control that derives from ListBox, that also supports cut, copy and paste operations. Although the execution of a copy operation on a ListBox will be coded differently to that of a TextBox, it would still be invoked from the same toolbar button.

So, my questions are:

[1] What needs to be implemented in a custom control in order that it can be commanded as in the above example? Specifically, it should be discoverable from a toolbar when focused.

[2] What markup is required in the toolbar to allow a single button to invoke different commands, depending on the type of control that is focused?

Many thanks for any advice you can offer.

Tim