C# 未知属性';转换器';对于类型。。。在我的定制装订中
我无法获得使用转换器的自定义绑定,在构建项目时得到: 分析标记扩展时遇到类型“MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension”的未知属性“Converter”错误2 错误指向以下代码:C# 未知属性';转换器';对于类型。。。在我的定制装订中,c#,wpf,xaml,C#,Wpf,Xaml,我无法获得使用转换器的自定义绑定,在构建项目时得到: 分析标记扩展时遇到类型“MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension”的未知属性“Converter”错误2 错误指向以下代码: <KeyBinding Key="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConve
<KeyBinding
Key="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Key}"
Modifiers="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Modifiers}"
Command="{Binding CopyToTargetCommand}"/>
转换器将字符串(如“Ctrl+Shift+X”)转换为键和修饰符:
private KeyGestureConverter mConverter = new KeyGestureConverter();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var text = value.ToString();
var gesture = mConverter.ConvertFromInvariantString(text) as KeyGesture;
if (parameter == "Key")
{
return gesture.Key;
}
if (parameter == "Modifiers")
{
return gesture.Modifiers;
}
return gesture;
}
我有什么遗漏吗?或者,在尝试从设置文件中的字符串绑定到KeyBinding时,我应该采取不同的方法吗
编辑:
使用以下代码,一切正常,但代码不可读。是否有一种方法可以自动生成此标记,因此我会在标记中编写,例如
<MyKeyBinding Value="CopyToTargetCommand"/>
它会产生其余的吗
<KeyBinding Command="{Binding CopyToTargetCommand}">
<KeyBinding.Key>
<helper:KeyboardShortcut Path="InsertTargetToSource" ConverterParameter="Key">
<helper:KeyboardShortcut.Converter>
<StaticResource ResourceKey="KeyGestureConverterKey"/>
</helper:KeyboardShortcut.Converter>
</helper:KeyboardShortcut>
</KeyBinding.Key>
<KeyBinding.Modifiers>
<helper:KeyboardShortcut Path="InsertTargetToSource" ConverterParameter="Modifiers">
<helper:KeyboardShortcut.Converter>
<StaticResource ResourceKey="KeyGestureConverterKey"/>
</helper:KeyboardShortcut.Converter>
</helper:KeyboardShortcut>
</KeyBinding.Modifiers>
</KeyBinding>
Adn然后在XAML中:
<helper:CustomizableKeyBinding Command="{Binding CopyToTargetCommand}" Description="..."/>
如果可能重复,有什么可以帮助我?我读了那里的链接,没有什么是适用的。
class CustomizableKeyBinding : KeyBinding
{
public CustomizableKeyBinding()
: base()
{
}
StringToKeyGestureConverter converter = new StringToKeyGestureConverter();
public string Description
{
set
{
InitBindings(value);
}
}
private void InitBindings(string value)
{
BindingOperations.SetBinding(this, KeyBinding.KeyProperty, new KeyboardShortcutExtension(value) { Converter = converter, ConverterParameter = "Key" });
BindingOperations.SetBinding(this, KeyBinding.ModifiersProperty, new KeyboardShortcutExtension(value) { Converter = converter, ConverterParameter = "Modifiers" });
}
}
<helper:CustomizableKeyBinding Command="{Binding CopyToTargetCommand}" Description="..."/>