Wpf 命令快捷方式文本字符串格式
我想通过显示快捷键命令来设置按钮工具提示Wpf 命令快捷方式文本字符串格式,wpf,Wpf,我想通过显示快捷键命令来设置按钮工具提示 <Button Command="Cut" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self},Path=Command}" ToolTip="{Binding RelativeSource={RelativeSource Se
<Button Command="Cut"
CommandTarget="{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},Path=Command}"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Command.InputGestures[0]}"/>
但是,它显示了以下结果:
它显示“Ctrl+X,Ctrl+X”。我只想要一个“Ctrl+X”。请帮帮我。我是初学者。我认为你应该保持简单
<Button Tooltip="Ctrl+X" Content="cut" Command="ApplicationCommands.Cut"
CommandBinding="textBox">
Xaml在上面。您的Xaml是正确的。我认为你在代码中使用的是这样的
command.InputGestures.Add(new KeyGesture(Key.X, ModifierKeys.Control));
你必须检查你的代码,可能是双重工作
我正在测试编写小代码。但这是正常的工作
也许转换器可以帮助:
<Button Command="ApplicationCommands.Cut"
Content="{Binding RelativeSource={RelativeSource Self},Path=Command}"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Command.InputGestures[0], Converter={StaticResource KeyGestureToStringConverter}}"/>
public class KeyGestureToStringConverter : IValueConverter
{
public static Conv Conv0() { return new Test.Conv(); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((KeyGesture)value).DisplayString;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
公共类keyGestureToString转换器:IValueConverter
{
公共静态Conv Conv0(){return new Test.Conv();}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回((键手势)值).DisplayString;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
我在这里找到了简单的方法
<Button Command="Cut"
DockPanel.Dock="Top"
CommandTarget="{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},Path=Command}"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Command.InputGestures[0].DisplayString}" />
但是我恐怕有些命令我不知道快捷键。没有程序代码我怎么能得到它?但是对于一些命令,我不知道快捷键。不直接设置快捷键,如何获取快捷键?谢谢。我忘了这件事。它可以工作,但现在我有了一个新的解决方案,不使用转换器。它只是在输入手势[0]之后添加.DisplayString。ToolTip=“{Binding RelativeSource={RelativeSource Self},路径=命令。输入手势[0]。显示字符串}”