我有一个 UserControl,奇怪的是,它将一堆其他控件和逻辑捆绑到一个整洁的小包中。它有一个接受 string
的 Text 属性。并做魔术,为用户显示结果。惊人的。
我从 TextBox 中获取该文本。用户将剪贴板中的文本粘贴到文本框中,该文本框绑定(bind)到我的 UserControl 上的 DP。
我想做的是去掉中间人并在我的 UserControl 中接受粘贴。
我已经尝试过使用 DataObject.Pasting附加事件,但这似乎不起作用。
你怎么做呢?
用我目前的解决方案回答了我自己的问题,但老实说它“闻起来”。如果有人有更好的答案,请添加它,如果它有效并且更好,我会选择它。
最佳答案
我的大脑开火了。 Command Bindings .现在我知道当有人试图粘贴并可以从那里拿走它时。
XAML:
<UserControl.CommandBindings>
<CommandBinding
Command="Paste"
Executed="CommandBinding_Executed"/>
</UserControl.CommandBindings>
(请原谅错误的代码;现在试图让它工作)和事件处理程序:
try
{
var text = Clipboard.GetData(DataFormats.Text) as string;
if (string.IsNullOrWhiteSpace(text))
return;
Lines = new Lines(text);
e.Handled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Paste failed", MessageBoxButton.OK);
}
这闻起来,恕我直言。但我不确定如何处理这个问题。
关于wpf - 如何在我的 UserControl 中接受剪贴板中的粘贴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3479756/