开发者

WPF - how do I extend (or work in parallel with) the built-in command mechanism?

Please excuse me if this is a dumb question - I hav开发者_StackOverflow社区e 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


Generic RelayCommands with MVVM Pattern are the best way to create a WPF Application. You can read about it here. These commands can be binded to any control by specifying the datacontext as the class of existence of the commands.

The markup would just use Binding keyword instead of x:Static.

Let me know if this solves your query.

Sushant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜