It's easy to add a commandbinding directly to the selector or Treeview commandbindings attribute to bind the application's Copy command: applicationcommands. copy: using this predefined WPF command, we do not need to define additional command types, nor do we need to define inputbinding to bind keys. Finally, use the clipboard. settext method in the Command Execution event to copy the value of the selected item to the clipboard.
Commandbinding can be declared in interface XAML, but it is better to write it as an additional property. Note that although the Treeview type defines many attributes similar to selector, such as selecteditem and selectedvalue, however, the Treeview type itself is not inherited from the selector type, and both of them are directly inherited from the itemscontrol type. Therefore, pay attention to processing the two types in the definition of additional properties, of course, each type of operation code is completely similar, because their attribute definitions are the same.
To use this function, you only need to set the selectorcopybehavior. isenabled additional attribute to true in selector or Treeview (Code download is available at the end of the article ).
<ListBox name = "list" Local: selectorcopybehavior. isenabled = "true">
After setting, press Ctrl + C after selecting a project from selector or Treeview to automatically copy the selectedvalue attribute of selector or Treeview to the clipboard.
However, there are still areas to be improved. As the basic knowledge of interface design, keyboard shortcuts can only be used as shortcuts for auxiliary operations, rather than the only way to execute operations, therefore, we 'd better define a right-click menu for selector or Treeview and add a copy menu item. This is also very simple. Bind The applicationcommands. Copy command directly. Take ListBox as an example. The Code is as follows:
<ListBox name = "list" Local: selectorcopybehavior. isenabled = "true">
<ListBox. Resources>
<Contextmenu X: Key = "listboxmenu">
<Menuitem header = "copy" command = "applicationcommands. Copy"/>
</Contextmenu>
</ListBox. Resources>
<ListBox. itemcontainerstyle>
<Style targettype = "listboxitem">
<Setter property = "contextmenu" value = "{staticresource listboxmenu}"/>
</Style>
</ListBox. itemcontainerstyle>
</ListBox>
After the program runs, you can copy the selected item by right-clicking the menu or shortcut.
Update:
V2
Added support for multiple ListBox selections.
Download Page
Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.
Program environment:. NET Framework 3.0
Source code environment: Microsoft Visual Studio express 2012 for Windows Desktop
V1
Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
File Type:. CS File