0

我想用以下 XAML 定义字典

Dictionary<Tuple<ModifierKeys, Key>, ICommand> 

我知道我需要创建继承 IDitionary 的类,如何调用它或在 XAML 中使用它

在用户控件级别,我想创建快捷方式列表,以传递给在 MVVM 模型中捕获 keydown 的全局行为,所以我想将此字典传递给 Behavior 以检查

4

1 回答 1

1

如果您的 xaml 支持它(我不是 Silverlight 方面的专家),您可以在 xaml 中使用 x:TypeArguments。

有关详细信息,请参阅此链接http://msdn.microsoft.com/en-us/library/ms750476(v=vs.100).aspx

如果您的 xaml 不支持它,实现此目的的一种非常简单的方法是创建您自己的类,该类将使用您想要的类型参数继承字典:

public class YourTuple
{
    public ModifierKeys ModifierKey { get; set; }
    public ICommand Command { get; set; }
}

public class YourClass : Dictionary<YourTuple, ICommand>

然后直接在你的 xaml 中使用它

<Window x:Class="SO.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:SO="clr-namespace:SO"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SO:YourClass>
            <YourCommand ...>
                <YourCommand.Key>
                    <YourTuple ModifierKey="..." Key="..."
                </YourCommand.Key>
            </YourCommand>
            ...
        </SO:YourClass>

    </Window.Resources>
</Window>
于 2012-11-05T09:48:32.300 回答