我不认为这可以用XAML方式完成。一个可能的解决方案是为此编写一个转换器。
XAML:
<Button Content="Picture" Command="{Binding TestCmd}">
<Button.InputBindings>
<KeyBinding Command="{Binding Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}">
<KeyBinding.Key>
<MultiBinding Converter="{StaticResource DictionaryMultiValueConverter}">
<Binding Path="Shortcuts" />
<Binding Path="Content" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Button}" />
</MultiBinding>
</KeyBinding.Key>
</KeyBinding>
</Button.InputBindings>
</Button>
和转换器:
public class DictionaryMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if(values.Length != 2)
throw new ArgumentException(@"DictionaryMultiValueConverter needs exactly two values", "values");
var dict = values[0] as IDictionary;
var key = values[1];
return dict != null && key != null && dict.Contains(key)
? dict[key]
: DependencyProperty.UnsetValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
转换得到你Dictionary
作为其第一个值,并选择作为其第二值的关键。然后它返回值为Dictionary
中传递的值。
谢谢。我在整个方法中发现的一个问题是,按钮必须专注于听按键。所以我要去一个完全不同的方向,并利用已经工作的定制按键。但再次感谢! –