gpt4 book ai didi

wpf - 如何使用路由命令的文本作为按钮内容

转载 作者:行者123 更新时间:2023-12-04 16:23:49 25 4
gpt4 key购买 nike

我在 View 上有一个按钮,该按钮通过 RoutedUICommand 绑定(bind)到 ViewModel 中定义的命令。

View 中的 XAML 代码摘录:

<Button Content="Login" Command="{Binding Login}" />

在 View 的 CodeBehind 中,我将 ViewModel 中的命令绑定(bind)添加到 View 的绑定(bind)集合中:
this.CommandBindings.Add( viewModel.LoginCommandBinding );

ViewModel 本身实现了命令:
public class LoginViewModel:ViewModelBase
{

public ICommand Login { get; private set; }
public CommandBinding LoginCommandBinding { get; private set; }

public LoginViewModel( ) {
this.Login =
new RoutedUICommand( "Login", "Login", typeof( Window ) );
this.LoginCommandBinding =
new CommandBinding( Login, LoginCommandHandler, CanExecuteHandler );
}

void LoginCommandHandler( object sender, ExecutedRoutedEventArgs e ) {
//Put code here
}

void CanExecuteHandler( object sender, CanExecuteRoutedEventArgs e ) {
return true;
}
}

所以命令是用文本和名称定义的“登录”。该按钮本身具有“登录”内容。有没有办法将命令的文本用作按钮的内容?

最佳答案

实现此目的的一个好方法是使用 RelativeSource 标记扩展将其绑定(bind)回来,例如 Command="Cmds:MyCommands.TestCmd" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"

关于wpf - 如何使用路由命令的文本作为按钮内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3769533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com