RibbonButton how to enable/disable depending on selected text in RTFBox

Dec 31, 2008 at 2:33 PM
Hello,

how can I set a RibbonButton enabled/disabled depending on selected text in a RTFBox? When some text is selected, the button should be enabled, otherwise disabled.

Regards
Carsten
Dec 31, 2008 at 6:27 PM
Hi Carsten,

You should add a CanExecute handler to your CommandBinding.  In the event handler you can set the CanExecute property of the event args, depending on the text selected in your RTFBox.

Alex
Jan 2, 2009 at 10:09 AM

Hello Alex,

I have already a CanExecute handler.  

 

<r:RibbonCommand x:Key="CutCommand"

 

CanExecute="OnCanExecute"

 

Executed="OnCut"

 

LabelTitle="Ausschneiden"

 

LargeImageSource="My Project\Images\Cut.png"

 

SmallImageSource="My Project\Images\Cut.png"

 

ToolTipTitle="Ausschneiden (Ctrl+X)"

 

ToolTipDescription="Markierung in die Zwischenablage ausschneiden"

 

ToolTipImageSource="My Project\Images\Cut.png" />

 

In OnCut I have just for testing

 

 

Private Sub OnCut(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

 

MessageBox.Show(

"Klick auf den Ausschneidenbutton!.", "Zwischenablage Ausschneiden")

 

 

End Sub

How should this work to enable/disable the button, if text is selected in a RTFBox? OnCut will only be executed when I press the button. Probably I don't understand the mechanism, so please give me a hint how to solve this.

Regards
Carsten

 

Jan 2, 2009 at 5:53 PM
Hi Carsten,

Try this code:

Private

 

Sub OnCanExecute(ByVal sender As Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)

 

 

If rtfBox.Selection.IsEmpty Then

 

e.CanExecute =

False

 

 

Else

 

e.CanExecute =

True

 

 

End If

 

 

End Sub

Hope this helps,
Alex

 

Jan 5, 2009 at 8:26 AM
Hello Alex,

thanks to your help. I got it running ;-)

Regards
Carsten