本文介绍了在WPF CommandConverter不能从System.String转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用.NET框架4.5
I have strange error in WPF using .NET Framework 4.5
<Window.CommandBindings>
<CommandBinding Command="ImportExcelCmd" CanExecute="ImportExcelCmd_CanExecute" Executed="ImportExcelCmd_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="I" Modifiers="Control" Command="ImportExcelCmd"></KeyBinding>
</Window.InputBindings>
我收到一个错误 CommandConverter不能从System.String转换
在哪里是我的错。
我还有一个绑定到ListView,如:
I have another binding to a ListView, like:
<ListView.CommandBindings>
<CommandBinding Command="Delete" CanExecute="Delete_CanExecute" Executed="Delete_Executed"></CommandBinding>
</ListView.CommandBindings>
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="Delete"></KeyBinding>
</ListView.InputBindings>
和它的作品。
推荐答案
如果你想使用 自定义选路命令
,您使用更详细的定义。
If you want to use Custom Routed commands
, you to use more verbose definition.
声明路由命令静态
类,然后用用它在XAML中的 X:静态
。
你可以参考答案。
Declare the routed command as static
in class and then use it in XAML using x:Static
.You can refer to the answer here.
有关答案的完整性考虑,我张贴的答案在这里的相关代码:
For the sake of completeness of the answer, I am posting the relevant code from the answer here:
namespace MyApp.Commands
{
public class MyApplicationCommands
{
public static RoutedUICommand ImportExcelCmd
= new RoutedUICommand("Import excel command",
"ImportExcelCmd",
typeof(MyApplicationCommands));
}
}
XAML
<Window x:Class="..."
...
xmlns:commands="clr-namespace:MyApp.Commands">
...
<Window.CommandBindings>
<CommandBinding
Command="{x:Static commands:MyApplicationCommands.ImportExcelCmd}"
CanExecute="ImportExcelCmd_CanExecute"
Executed="ImportExcelCmd_Executed" />
</Window.CommandBindings>
这篇关于在WPF CommandConverter不能从System.String转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!