I am unable to get a custom binding working with a converter, getting this when building the project:
Error 2 Unknown property ‘Converter’ for type ‘MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension’ encountered while parsing a Markup Extension.
The error points to this code:
<KeyBinding
Key="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Key}"
Modifiers="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Modifiers}"
Command="{Binding CopyToTargetCommand}"/>
KeyboardShortCut is a binding from a Settings file :
public class KeyboardShortcutExtension : Binding
{
public KeyboardShortcutExtension()
{
Initialize();
}
public KeyboardShortcutExtension(string path)
: base(path)
{
Initialize();
}
private void Initialize()
{
this.Source = TI.Shortcuts.Default;
this.Mode = BindingMode.TwoWay;
}
}
And the converter converts from string (like “Ctrl+Shift+X”) to Key and Modifiers:
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;
}
Is there something I am missing? Or should I take a different approach when trying to bind from a string in Settings file to a KeyBinding?
EDIT:
Using the following code, everything works fine, but the code is not readable. Is there a way of generating this automatically, so in my markup I would write e.g. just
<MyKeyBinding Value="CopyToTargetCommand"/>
and it would generate the rest?
<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 then in XAML: