使用绑定转换器:
public class CommandParamConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
return string.Format("Key {0}", value);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
将其添加到 Windows\UserControl 资源:
<Window.Resources>
<local:CommandParamConverter x:Key="commandParamConverter" />
</Window.Resources>
在菜单命令参数绑定中引用它:
<MenuItem Header="Key" CommandParameter="{Binding Converter={StaticResource commandParamConverter}, Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>