locked
Binding richeditbox in xaml from codebehind

    Question

  • Hello,

    How to bind richeditbox inside xaml. It is in datatemplate of gridview


    • Edited by zee_patel Friday, February 21, 2014 9:07 AM
    Friday, February 21, 2014 7:15 AM

Answers

  • See my blog entry Binding HTML to a WebView with Attached Properties.

    The same technique can be used to bind RTF to a RichEditBox.

    If that isn't what you're looking for then please provide more details on your specific scenario.

    --Rob

    • Marked as answer by zee_patel Monday, February 24, 2014 6:01 AM
    Friday, February 21, 2014 4:09 PM
    Owner
  • Yes, that's what I was working on. I have a code sample:

    MainPage.XAML.CS:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                List<string> MyList = new List<string>();
                MyList.Add("A");
                MyList.Add("B");
                MyList.Add("C");
                MyList.Add("D");
                MyList.Add("E");
                MyListView.ItemsSource = MyList;
            }
        }

    MainPage.Xaml:

    <Page
        x:Class="REBAttachedProperty.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:REBAttachedProperty"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="MyListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <RichEditBox Width="50" Height="30" local:BindableREBText.Text="{Binding}" ></RichEditBox>                    
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Page>

    BindableREBText.cs:

    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace REBAttachedProperty
    {
        class BindableREBText
        {
    
            public static DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
              "Text",
              typeof(string),
              typeof(BindableREBText),
              new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged))
            );
    
            public static void SetText(RichEditBox REB, string value)
            {
                REB.SetValue(TextProperty, value);
            }
    
            public static string GetText(RichEditBox REB)
            {
                return (string) REB.GetValue(TextProperty);
            }
    
            public static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                RichEditBox REB = sender as RichEditBox;
                string S = args.NewValue as string;
                REB.Document.SetText(Windows.UI.Text.TextSetOptions.None, S);
            }
        }
    }
    


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by zee_patel Monday, February 24, 2014 6:01 AM
    Friday, February 21, 2014 6:21 PM
    Moderator

All replies

  • I'm working on this...

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 4:01 PM
    Moderator
  • See my blog entry Binding HTML to a WebView with Attached Properties.

    The same technique can be used to bind RTF to a RichEditBox.

    If that isn't what you're looking for then please provide more details on your specific scenario.

    --Rob

    • Marked as answer by zee_patel Monday, February 24, 2014 6:01 AM
    Friday, February 21, 2014 4:09 PM
    Owner
  • Yes, that's what I was working on. I have a code sample:

    MainPage.XAML.CS:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                List<string> MyList = new List<string>();
                MyList.Add("A");
                MyList.Add("B");
                MyList.Add("C");
                MyList.Add("D");
                MyList.Add("E");
                MyListView.ItemsSource = MyList;
            }
        }

    MainPage.Xaml:

    <Page
        x:Class="REBAttachedProperty.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:REBAttachedProperty"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="MyListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <RichEditBox Width="50" Height="30" local:BindableREBText.Text="{Binding}" ></RichEditBox>                    
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Page>

    BindableREBText.cs:

    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace REBAttachedProperty
    {
        class BindableREBText
        {
    
            public static DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
              "Text",
              typeof(string),
              typeof(BindableREBText),
              new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged))
            );
    
            public static void SetText(RichEditBox REB, string value)
            {
                REB.SetValue(TextProperty, value);
            }
    
            public static string GetText(RichEditBox REB)
            {
                return (string) REB.GetValue(TextProperty);
            }
    
            public static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                RichEditBox REB = sender as RichEditBox;
                string S = args.NewValue as string;
                REB.Document.SetText(Windows.UI.Text.TextSetOptions.None, S);
            }
        }
    }
    


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by zee_patel Monday, February 24, 2014 6:01 AM
    Friday, February 21, 2014 6:21 PM
    Moderator
  • Yes, that's what I was working on. I have a code sample:

    MainPage.XAML.CS:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                List<string> MyList = new List<string>();
                MyList.Add("A");
                MyList.Add("B");
                MyList.Add("C");
                MyList.Add("D");
                MyList.Add("E");
                MyListView.ItemsSource = MyList;
            }
        }

    MainPage.Xaml:

    <Page
        x:Class="REBAttachedProperty.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:REBAttachedProperty"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="MyListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <RichEditBox Width="50" Height="30" local:BindableREBText.Text="{Binding}" ></RichEditBox>                    
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Page>

    BindableREBText.cs:

    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace REBAttachedProperty
    {
        class BindableREBText
        {
    
            public static DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
              "Text",
              typeof(string),
              typeof(BindableREBText),
              new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged))
            );
    
            public static void SetText(RichEditBox REB, string value)
            {
                REB.SetValue(TextProperty, value);
            }
    
            public static string GetText(RichEditBox REB)
            {
                return (string) REB.GetValue(TextProperty);
            }
    
            public static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                RichEditBox REB = sender as RichEditBox;
                string S = args.NewValue as string;
                REB.Document.SetText(Windows.UI.Text.TextSetOptions.None, S);
            }
        }
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    This is one-way binding. View Model can notify View, but View can not notify View Model.

    Do You know how to improve this code to two-way binding?


    • Edited by OliwiaRusek Saturday, March 7, 2015 7:47 PM
    Saturday, March 7, 2015 3:02 PM