WPF 功能区按钮

发布于 2024-08-27 13:12:41 字数 291 浏览 4 评论 0原文

如何以编程方式设置 RibbonButton 上的文本?现在我有下面的代码,但按钮不显示“浏览”。有什么建议吗?

RibbonButton btn = new RibbonButton();
btn.Name = "btnBrowse";
btn.Content = "Browse";
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
btn.Click += new RoutedEventHandler(btn_Click);

How can I programatically set the text on a RibbonButton? Right now I have the code below, but the button does not display 'Browse'. Any suggestions?

RibbonButton btn = new RibbonButton();
btn.Name = "btnBrowse";
btn.Content = "Browse";
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
btn.Click += new RoutedEventHandler(btn_Click);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-09-03 13:12:41

RibbonControlsLibrary 中的 RibbonButton 的行为与标准 WPF 按钮不同,并且需要命令来显示文本。您还可以在该命令中分配图像和其他项目(例如工具提示)。

var cmd = new RibbonCommand();
cmd.LabelTitle = "Browse";
cmd.CanExecute += ( sender, args ) => args.CanExecute = true;
cmd.Executed +=new ExecutedRoutedEventHandler(cmd_Executed);

var btn = new RibbonButton();
btn.Command = cmd;

MyRibbonGroup.Controls.Add( btn );

您必须为 CanExecute 指定 true,否则命令/按钮将始终被禁用。 CanExecute 方法还可以让您的业务逻辑禁用或启用命令/按钮。

RibbonButtons from the RibbonControlsLibrary behave differently than standard WPF buttons and need a command to display text. The command is where you also assign the images and other items such as tool tips.

var cmd = new RibbonCommand();
cmd.LabelTitle = "Browse";
cmd.CanExecute += ( sender, args ) => args.CanExecute = true;
cmd.Executed +=new ExecutedRoutedEventHandler(cmd_Executed);

var btn = new RibbonButton();
btn.Command = cmd;

MyRibbonGroup.Controls.Add( btn );

You must assign true to CanExecute, otherwise will the command/button will always be disabled. The CanExecute method can have your business logic disable or enable the command/button as well.

盗梦空间 2024-09-03 13:12:41

WPF 按钮是与 WPF 中的其他所有内容一样的容器。创建一个 TextBlock 并将其设置为按钮的内容:

RibbonButton btn = new RibbonButton(); 
btn.Name = "btnBrowse"; 
TextBlock btnText = new TextBlock();
btnText.Text = "Browse";
btn.Content = btnText; 
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
btn.Click += new RoutedEventHandler(btn_Click);

也就是说,我强烈建议您考虑在 XAML 中构建 UI。如果文本在运行时会发生变化,请对按钮的文本进行数据绑定。

WPF Buttons are containers like just about everything else in WPF. Create a TextBlock and set it as the Content of your button:

RibbonButton btn = new RibbonButton(); 
btn.Name = "btnBrowse"; 
TextBlock btnText = new TextBlock();
btnText.Text = "Browse";
btn.Content = btnText; 
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
btn.Click += new RoutedEventHandler(btn_Click);

That said, I hightly suggest you consider building your UI in XAML. If the text will change during runtime, databind the text of the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文