Enable button when RichTextEditor Contains Text

2 Answers 80 Views
RichTextEditor
Sam
Top achievements
Rank 1
Sam asked on 28 Dec 2021, 05:37 PM
Does anyone have a suggestion for binding the state of a button to whether the RichTextEditor contains text? I don't want the button to be enabled unless there is content in the rich text editor.

2 Answers, 1 is accepted

Sort by
0
Didi
Telerik team
answered on 29 Dec 2021, 09:03 AM

Hello,

You can use a converter and bind it to the Button IsEnabled property. 

For example: 

Page's definition

<ContentPage.Resources>
    <local:SourceToBoolConverter x:Key="SourceToBoolConverter"/>
</ContentPage.Resources>
<Grid RowDefinitions="Auto,Auto,*">
    <Button Text="Button" IsEnabled="{Binding Path=Source, Source={x:Reference richTextEditor}, Converter={StaticResource SourceToBoolConverter}}"/>


    <telerikRichTextEditor:RadRichTextEditorToolbar x:Name="richTextToolbar" Grid.Row="1"  RichTextEditor="{x:Reference richTextEditor}" />
    <telerikRichTextEditor:RadRichTextEditor x:Name="richTextEditor" Grid.Row="2" />

</Grid>

 

And the RichTexteditor Source: 

var htmlSource = @"<h4>One of the Most Beautiful Islands on Earth - Tenerife</h4>";
this.richTextEditor.Source = RichTextSource.FromString(htmlSource);
And the converter:
public class SourceToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return false; 
        }
        else return true;   
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

This is just an approach you can use, you may need to extend the logic further to match the exact scenario. Of course you can search for alternative solution.

Regards,
Didi
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
hunters
Top achievements
Rank 1
Iron
answered on 19 Jan 2022, 08:39 AM | edited on 19 Jan 2022, 08:46 AM
This is a complicated answer. If I hadn't read your post first, I would never have figured it out
Tags
RichTextEditor
Asked by
Sam
Top achievements
Rank 1
Answers by
Didi
Telerik team
hunters
Top achievements
Rank 1
Iron
Share this question
or