tags:

views:

292

answers:

2
+1  Q: 

WPF RibbonButton

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);
+1  A: 

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.

Randolpho
RibbonButtons from the RibbonControlsLibrary behave differently than standard WPF buttons. See my answer.
Metro Smurf
Unfortunately, it doesn't work.. they seem to have a different behavior than normal buttons
melculetz
@Metro Smurf: interesting, thanks for the comment and the other answer; I was unaware of that. I'm going to keep mine around so anyone swinging by can see the difference.
Randolpho
Hopefully the Ribbon team will release an update soon that will resolve many of these differences, as well as bug-fixes.
Metro Smurf
+3  A: 

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.

Metro Smurf
You are right, if you have the button inside a RibbonGroup. But if you have it in a RibbonControlGroup (as I had it), it doesn't work
melculetz