将属性复制到剪贴板 [英] Copy Property to Clipboard

查看:149
本文介绍了将属性复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串属性在我的ViewModel / Datacontext和想要一个简单的按钮,将其内容复制到剪贴板。这是可能从XAML做,或者我需要处理按钮单击事件(或使用ICommand)来完成这个?我认为以下会工作,但我的按钮总是灰色:

I have a string property in my ViewModel/Datacontext and want a simple button that copies its contents to the clipboard. Is this possible to do from XAML, or I do I need to handle the button click event (or use an ICommand) to accomplish this? I thought the following would work, but my button is always greyed out:

    <Button Width="100" Content="Copy" Command="ApplicationCommands.Copy" 
CommandTarget="{Binding MyStringProperty}"/>


推荐答案

ApplicationCommands希望在工具栏或菜单这将给予他们基于RoutedUICommands的FocusScope。如果您的按钮位于工具栏或菜单之外,则需要明确声明焦点范围:

The ApplicationCommands are expecting to be in a Toolbar or Menu which will give them FocusScope based on RoutedUICommands. If your button is outside a Toolbar or Menu, then you need to explicitly declare the focus scope:

<Button 
  Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
  Command="ApplicationCommands.Copy" 
  FocusManager.IsFocusScope="True"/>

CommandTarget用于声明哪个元素将提供FocusScope,这意味着复制按钮只会当在CommandTarget中声明的元素具有焦点时启用,并且在复制的情况下具有突出显示的文本:

The CommandTarget is used to declare which element will provide the FocusScope which means that the Copy button will only be enabled whenever the element declared in the CommandTarget has focus, and in the case of copy, has text highlighted:

<Button 
  Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
  Command="ApplicationCommands.Copy"
  CommandTarget="{Binding ElementName=MyElement}" />

回答你的具体问题,你需要拦截ApplicationCommands.Copy命令来获取/设置ViewModel的MyStringProperty;说实话,我不知道在哪里甚至开始计算一个。也许有人在这里更聪明可以提供这一块拼图。

In answer to your specific question, you'd need to intercept the ApplicationCommands.Copy command to get/set your ViewModel's MyStringProperty; and to be honest, I'm not sure where to even start to figure that one out. Maybe someone smarter around here could provide that piece of the puzzle.

这篇关于将属性复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆