簡體   English   中英

我的自定義綁定中類型…的未知屬性“轉換器”

[英]Unknown property 'Converter' for type … in my custom Binding

我無法得到一個轉換器自定義綁定工作,建設項目時得到這樣的:

錯誤2解析標記擴展時遇到類型'MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension'的未知屬性'Converter'。

錯誤指向此代碼:

<KeyBinding 
        Key="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Key}" 
        Modifiers="{helper:KeyboardShortcut InsertTargetToSource, Converter={StaticResource KeyGestureConverterKey},ConverterParameter=Modifiers}" 
        Command="{Binding CopyToTargetCommand}"/>

KeyboardShortCut是來自Settings文件的綁定:

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;
    }

}

轉換器將字符串(例如“ 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>
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" });
    }
}

然后在XAML中使用Adn:

 <helper:CustomizableKeyBinding Command="{Binding CopyToTargetCommand}" Description="..."/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM