Wpf 如何创建具有更改控件标题值功能的RelayCommand?
RouteDuicCommand具有更改控件标题值的功能 例如,下面的代码将MenuItem的标题值更改为“Cut” 有人能告诉我该怎么解决这个问题吗Wpf 如何创建具有更改控件标题值功能的RelayCommand?,wpf,command,Wpf,Command,RouteDuicCommand具有更改控件标题值的功能 例如,下面的代码将MenuItem的标题值更改为“Cut” 有人能告诉我该怎么解决这个问题吗 感谢阅读。当MenuItem.Command绑定到RoutedUICommand并且设置了RoutedUICommand.Text属性时,会自动设置MenuItem.Header值 因此,您必须使用RoutedUICommand并设置Text属性。然后将调用委托给您的RelayCommand实例: public partial class MyU
感谢阅读。当
MenuItem.Command
绑定到RoutedUICommand
并且设置了RoutedUICommand.Text
属性时,会自动设置MenuItem.Header
值
因此,您必须使用RoutedUICommand
并设置Text
属性。然后将调用委托给您的RelayCommand
实例:
public partial class MyUserControl : UserControl
{
public static RoutedUICommand NewProjectRoutedUICommand = new RoutedUICommand() { Text = "Create New Project" };
// Perhaps this command is exposed by a view model
public static RelayCommand NewProjectRelayCommand = new RelayCommand(NewProjectExecute, NewProjectCanExecute);
public MyUserControl()
{
this.CommandBindings.Add(new CommandBinding(NewProjectCommand, ExecutedNewProjectRoutedUICommand, CanExecuteNewProjectRoutedUICommand));
}
private void ExecutedNewProjectRoutedUICommand(object sender, ExecutedRoutedEventArgs e)
{
// Delegate to RelayCommand instance
MyUserControl.NewProjectRelayCommand.Execute(e.Parameter);
}
private bool CanExecuteNewProjectRoutedUICommand(object sender, CanExecuteRoutedEventArgs e)
{
// Delegate to RelayCommand instance
return MyUserControl.NewProjectRelayCommand.CanExecute(e.Parameter);
}
}
用法
<MenuItem Command="{x:Static MyUserControl.NewProjectRoutedUICommand" />
<MenuItem Command="{x:Static MenuActionCommands.NewProject}"
Header="{x:Static MenuActionCommands.NewProject.Text}" />
用法
<MenuItem Command="{x:Static MyUserControl.NewProjectRoutedUICommand" />
<MenuItem Command="{x:Static MenuActionCommands.NewProject}"
Header="{x:Static MenuActionCommands.NewProject.Text}" />
也许RouteDuicCommand()的官方源代码可以帮助您。。。顺便问一下,您的relayicommand的用法是否类似于“RoutedUICommand,但执行我想要的操作”或“RelayCommand,但更改它绑定到的元素的标题”?谢谢您的评论。我想要的是第二种意见。换句话说,我想要“RelayCommand,但更改它绑定到的元素的头”。我不想要路线功能。嗯。。。你能给我举个替代的例子吗?我想在XAML中使用like。但是上面的代码似乎无法做到这一点,因为它必须在UserControl.Yes中定义命令。这需要添加定义
RoutedUiCommand
。由于此命令是静态的
,只要definig类型扩展DependencyObject
,您就可以在任何地方定义它。但我可以更新我的答案,向您展示替代解决方案。我添加了另一个选项。使用样式时
可以“自动”分配标题
。
public class RelayUICommand : RelayCommand
{
public RelayUICommand(Action<object> executeDelegate, Func<object, bool> canExecuteDelegate, string description) : base(executeDelegate, canExecuteDelegate)
{
this.Text = description;
}
public RelayUICommand(Action<object> executeDelegate, Func<object, bool> canExecuteDelegate) => this(executeDelegate, canExecuteDelegate, string.Empty);
public string Text { get; set; }
}
<MenuItem Command="{x:Static MenuActionCommands.NewProject}"
Header="{x:Static MenuActionCommands.NewProject.Text}" />
<!-- Use in conjunction with the RelayUICommand -->
<Style TargetType="MenuItem">
<Setter Property="Header"
Value="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text} />
</Style>
<MenuItem Command="{x:Static MenuActionCommands.NewProject}" />