WPF RibbonButton
How can I programatically set the text on a RibbonButton?开发者_Python百科 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);
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.
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.
精彩评论