This is a migrated thread and some comments may be shown as answers.

Binding to XamlDataProvider not working

15 Answers 240 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 11 Mar 2011, 12:56 PM
Having followed your example to bind a XamlDataProvider to a RadRichTextBox, nothing is appearing. The Xaml property of the XamlDataProvider is bound to the Text property of a textbox, and the text is definitely a valid RadDocument (it's the output of exporting a raddocument to xaml using the XamlFormatProvider). The RichTextBox property is also bound correctly, so why isn't it working?

code:
<UserControl.Resources>
        <TelerikXaml:XamlDataProvider x:Key="DocumentDataSouce" RichTextBox="{Binding ElementName=EditBox}" Xaml="{Binding Path=Text, ElementName=RichTextBoxDataSource}"/>
    </UserControl.Resources>
 
    <Grid Margin="10">
         
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
 
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
         
        <Border CornerRadius="10, 10, 0, 0" Background="{StaticResource OrangeGradient}" BorderThickness="1" BorderBrush="{StaticResource DarkOrange}" Grid.Row="0">
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource DarkOrangeHeaderText}" Text="Edit Report"/>
        </Border>
         
        <Border Background="White" Grid.Row="1" Grid.RowSpan="2"/>
         
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <Button x:Name="ExportButton" Content="Export" Click="ExportButton_Click"/>
            <Button x:Name="ImportButton" Content="Import" Margin="10, 0, 0, 0" Click="ImportButton_Click"/>
            <TextBox x:Name="RichTextBoxDataSource" Margin="10, 0, 0, 0"/>
        </StackPanel>
         
         
 
        <Telerik:RadRichTextBox x:Name="EditBox" Grid.Row="2"/>
 
    </Grid>

15 Answers, 1 is accepted

Sort by
0
hwsoderlund
Top achievements
Rank 1
answered on 11 Mar 2011, 02:27 PM
You need to set Mode=TwoWay on your binding.
0
Philip
Top achievements
Rank 1
answered on 11 Mar 2011, 03:17 PM
Already tried that, doesn't work
0
Ivailo Karamanolev
Telerik team
answered on 16 Mar 2011, 05:37 PM
Hello hwsoderlund,

We haven't encountered any issues with XamlDataProvider itself yet. Please provide us with a sample project that demonstrates the problem so we can give you further advice.

Greetings,
Ivailo
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
hwsoderlund
Top achievements
Rank 1
answered on 16 Mar 2011, 07:03 PM
Hi Ivailo. For once I am not the one with problems. :) It is Philip you should address.
0
hwsoderlund
Top achievements
Rank 1
answered on 18 Mar 2011, 09:55 AM
I just now stumbled upon a situation that causes the exact symptoms that Philip describes, and that used to work before (sorry, I cannot be more specific than that, I do not know when it stopped working). I do not have time to post a sample project right now, but this occurs when the object being bound to is null at the time of loading (and rendering) the radrichtextbox. Example:

MainPage.xaml contains a radrichtextbox with a xamldataprovider. The xamldataprovider binds its Xaml property like so:
Xaml="{Binding XamlContent, Mode=TwoWay}"

When the page is loaded, the DataContext of MainPage is null. A service call is then initated to pick up a XamlEntity with the property XamlContent. This object is then set as the DataContext of MainPage when it is returned from the service. But when it is set, the xamldataprovider will not react to the fact that there is suddenly an object to bind to. I am 100% certain that this used to work.

I can quite easily work around the problem by creating an empty XamlEntity with an empty string for the XamlContent in the constructor of MainPage. Then when the object is loaded from the server I just populate the existing DataContext with the values from the new XamlEntity. But I wish I would not have to. I am guessing that this is also the cause of Philips problems. @Philip: Can you confirm this?
0
Iva Toteva
Telerik team
answered on 23 Mar 2011, 12:50 PM
Hello hwsoderlund,

Thank you for taking the time to investigate the issue.
We managed to reproduce the behavior on our side and the problem is indeed present in the scenario you are describing - when changing the DataContext that the data provider uses. It seems that in this case the binding expression is not automatically updated and therefore, the document does not show the new data.
However, since Q1, all data providers are FrameworkElements, which allows you to put them in the visual tree along with the RadRichTextBox. This enables the Silverlight runtime to properly update the binding expression on the data provider. This will not change anything whatsoever in the appearance, as data providers have no visual representation, but the binding will work correctly upon changing the DataContext.

Kind regards,
Iva
the Telerik team
0
hwsoderlund
Top achievements
Rank 1
answered on 23 Mar 2011, 02:41 PM
Ok, that is a nice improvement. And I can confirm that it works as expected.
0
Daní
Top achievements
Rank 1
answered on 25 Mar 2011, 09:57 AM
Hello,

When debugging, I usually set VS to break each time an exception is thrown, also if is a handled one, and I also set "Disable just my code" in IntelliTrace. Well, since upgrade to Q1 2011 release, VS breaks continually due a handled exception produced when RichTextBox imports the document ising a XamlDataProvider. I'm using RichTextBox in readonly mode inside a paged ListBox, as page size is 8, VS breaks 8 times. 

As I said, this is a handled exception, unsetting VS to break on throw exceptions an enabling "just my code" in IntelliTrace avoids VS to halt, but I think this exception is slowing down the UI, I can notice that loading the ListBox is slower than before Q1 2011 release. I'm pasting exception call stack:

Telerik.Windows.Documents!Telerik.Windows.Documents.Model.RadDocument.RebuildListDataOld(Telerik.Windows.Documents.Model.RadDocument document) + 0x77 bytes
  Telerik.Windows.Documents!Telerik.Windows.Documents.Model.RadDocument.RebuildListsData(Telerik.Windows.Documents.Model.RadDocument document) + 0x1e bytes
  Telerik.Windows.Documents.FormatProviders.Xaml!Telerik.Windows.Documents.FormatProviders.Xaml.XamlFormatProvider.Import(string input) + 0x9e bytes
  Telerik.Windows.Documents!Telerik.Windows.Documents.FormatProviders.DataProviderBase.UpdateDocument() + 0x5f bytes
  Telerik.Windows.Documents!Telerik.Windows.Documents.FormatProviders.DataProviderBase.RichTextBoxChangedCallback(System.Windows.DependencyObject sender, System.Windows.DependencyPropertyChangedEventArgs e) + 0x101 bytes
  Telerik.Windows.Controls!Telerik.Windows.PropertyMetadata.Create.AnonymousMethod__0(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) + 0x283 bytes
  System.Windows.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp, object oldValue, object newValue) + 0x51 bytes
  System.Windows.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) + 0xf6 bytes
  System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet) + 0x2ad bytes
  System.Windows.dll!System.Windows.Data.BindingOperations.SetBinding(System.Windows.DependencyObject target, System.Windows.DependencyProperty dp, System.Windows.Data.BindingBase binding) + 0xd1 bytes
  System.Windows.dll!System.Windows.Data.Binding.MS.Internal.IMarkupExtension.SetupExtension(object target, object property) + 0x56 bytes
  System.Windows.dll!MS.Internal.XamlManagedRuntimeRPInvokes.TryApplyMarkupExtensionValue(object target, MS.Internal.XamlPropertyToken propertyToken, object value) + 0x7d bytes
  System.Windows.dll!MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(MS.Internal.XamlTypeToken inType, ref MS.Internal.XamlQualifiedObject inObj, MS.Internal.XamlPropertyToken inProperty, ref MS.Internal.XamlQualifiedObject inValue) + 0x5e bytes
  [Managed to Native Transition]
  System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element, System.Windows.Size availableSize) + 0x62 bytes
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize) + 0x18 bytes
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, float inWidth, float inHeight, out float outWidth, out float outHeight) + 0xa3 bytes
  [Managed to Native Transition]
  System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element, System.Windows.Size availableSize) + 0x62 bytes
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize) + 0x18 bytes
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, float inWidth, float inHeight, out float outWidth, out float outHeight) + 0xa3 bytes
  [Appdomain Transition]

0
Ivailo Karamanolev
Telerik team
answered on 29 Mar 2011, 09:52 AM
Hi Daní,

What's happening in your case is most probably that the first time, the XamlFormatProvider is passed an empty string. As the empty string is not valid XAML, the XamlFormatProvider throws an exception, the fact that it comes from RebuildListsData is just a side effect. You can always initialize your property to some predefined value, e.g. an empty document exported to XAML and avoid the exception if you deem necessary.

All the best,
Ivailo
the Telerik team
0
Daní
Top achievements
Rank 1
answered on 29 Mar 2011, 07:01 PM
Hi Ivailo,

XamlText property (in my ViiewModel object) never is empty. Debugging the application I may observe the exception is thrown before XamlDataProvider tries to get XamlText property in its DataContext object. I've placed a break point at XamlText property get accessor. Firstly, exception is thrown and then debugger breaks on the XamlText property. This didn't occur before Q1 2011 release. Walking by the exception call stack I can see a Telerik.Windows.Documents.FormatProviders.DataProviderBase.RichTextBoxChangedCallback  method call that originates a DataProviderBase.UpdateDocument call. As now XamlDataProviderBase is a control, it seems the RichTextBoxChangedCallback is called before OnApplyTemplate and control is trying to format Xaml property when is not bound yet.

As I mentioned previously, I'm using a RichTextBox in readonly mode inside a ListBox, I'm pasting the DataTemplate used for ListBox ItemTemplate, may this may help:

<DataTemplate x:Key="AnnotationViewTemplate">
        <Border BorderBrush="Black" BorderThickness="0,0,0,2" Padding="0,0,0,5">
            <Grid x:Name="LayoutRoot">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <telerik:RadWrapPanel Grid.Row="0">
                    <telerik:RadRibbonButton VerticalAlignment="Center" Command="{Binding Path=CopyCommand, ElementName=copyBehavior}" 
                                             telerik:ScreenTip.Title="{Binding Path=CopyAnnotationTitle, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding Path=CopyAnnotationDescription, Source={StaticResource Strings}}" CollapseToSmall="WhenGroupIsMedium" Size="Medium" 
                                             SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/copy.png"  FontSize="9.333" Margin="0,0,5,0"/>
                    <StackPanel Margin="5,5,15,5" Orientation="Horizontal" Visibility="{Binding FormattedDate, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Path=AnnotationDateLabel, Source={StaticResource Strings}}" Margin="0,0,6,0" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataHighlightTextBlock}"  Text="{Binding FormattedDate}" telerik:ScreenTip.Title="{Binding Path=AnnotationDateLabel, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding FormattedDate}"/>
                    </StackPanel>
                    <StackPanel Margin="5,5,15,5" Orientation="Horizontal" Visibility="{Binding Responsible, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding AnnotationResponsibleLabel, Source={StaticResource Strings}}" Margin="0,0,6,0" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock  Style="{StaticResource DataHighlightTextBlock}" Text="{Binding Responsible}" telerik:ScreenTip.Title="{Binding AnnotationResponsibleLabel, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding Responsible}"/>
                          
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" Visibility="{Binding SpecialityDescription, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding AnnotationSpecialityLabel, Source={StaticResource Strings}}" Margin="0,0,6,0" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataTextStyleWordEllipses}" Text="{Binding SpecialityDescription}" telerik:ScreenTip.Title="{Binding Path=AnnotationSpecialityTitle, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding SpecialityDescription}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5,5,15,5" Visibility="{Binding Episode, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Path=AnnotationEpisodeLabel, Source={StaticResource Strings}}" Margin="0,0,6,0" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataHighlightTextBlock}"  Text="{Binding Episode}" telerik:ScreenTip.Title="{Binding Path=AnnotationEpisodeLabel, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding Episode}" />
                          
                    </StackPanel>
  
                    <StackPanel Orientation="Horizontal" Margin="5,5,15,5" Visibility="{Binding TreatmentUnityDescription, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding AnnotationTreatmentUnityDescriptionLabel, Source={StaticResource Strings}}" Margin="0,0,6,0" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataTextStyleWordEllipses}" Text="{Binding TreatmentUnityDescription}" telerik:ScreenTip.Title="{Binding Path=TreatmentUnityTitle, Source={StaticResource Strings}}"   telerik:ScreenTip.Description="{Binding TreatmentUnityDescription}"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" Visibility="{Binding ServiceText, Converter={StaticResource TextLengthToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Margin="0,0,6,0" Text="{Binding Path=AnnotationServiceLabel, Source={StaticResource Strings}}" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataHighlightTextBlock}" Text="{Binding ServiceText}" telerik:ScreenTip.Title="{Binding Path=AnnotationServiceLabel, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding ServiceText}"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" Visibility="{Binding Path=IsDisabled, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <TextBlock TextWrapping="Wrap" Margin="0,0,6,0" Text="{Binding Path=AnnotationDisablingDateLabel, Source={StaticResource Strings}}" Style="{StaticResource LabelTextStyle}"/>
                        <TextBlock Style="{StaticResource DataHighlightTextBlock}"  Text="{Binding FormattedDisablingDate}" telerik:ScreenTip.Title="{Binding Path=AnnotationDisablingDateLabel, Source={StaticResource Strings}}" telerik:ScreenTip.Description="{Binding FormattedDisablingDate}"/>
                    </StackPanel>
                </telerik:RadWrapPanel>
                <telerik:RadRichTextBox BorderThickness="0" Grid.Row="1" IsSelectionMiniToolBarEnabled="False" HorizontalContentAlignment="Stretch" IsContextMenuEnabled="True" DocumentInheritsDefaultStyleSettings="True" FontFamily="Verdana" FontSize="12" IsSpellCheckingEnabled="False" Width="Auto" VerticalScrollBarVisibility="Disabled" x:Name="RichTextBox" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled">
                    <i:Interaction.Behaviors>
                        <localBehaviors:RichTextBoxCopyBehavior x:Name="copyBehavior"/>
                    </i:Interaction.Behaviors>
                </telerik:RadRichTextBox>
                <telerikXaml:XamlDataProvider Grid.Row="1" Xaml="{Binding Path=XamlText, Mode=TwoWay}" RichTextBox="{Binding ElementName=RichTextBox}"  ccBehaviors:DefaultLineSpacingBehavior.DefaultLineSpacing="1"/>
            </Grid>
        </Border>
    </DataTemplate>
0
Daní
Top achievements
Rank 1
answered on 30 Mar 2011, 07:29 PM
Hello,

I think I can confirm my previous post. It seems there's an issue on XamlDataProvider initialization. The following code produces a handled exception :
public class Test
{
public string XamlText { get; set; }
 
        public RadRichTextBox RichTextBox { get; set; }
        public void UpdateXaml()
        {
            var xamlProvider = new XamlDataProvider { RichTextBox = RichTextBox };
            xamlProvider.Xaml = XamlText;
xamlProvider.UpdateDocument();
        }
    }

This code does not produce the handled exception:
public class Test
    {
        public string XamlText { get; set; }
 
        public RadRichTextBox RichTextBox { get; set; }
        public void UpdateXaml()
        {
            var xamlProvider = new XamlDataProvider { Xaml = XamlText, RichTextBox = RichTextBox };
            xamlProvider.UpdateDocument();
        }
    }
0
Ivailo Karamanolev
Telerik team
answered on 31 Mar 2011, 10:08 AM
Hi Daní,

You are correct in the statement that when you change the value of the RichTextBox property of the data provider, a call to UpdateDocument is made, which throws a handled exception. This has been changed in Q1 to achieve behavior more like the one standard controls. If the handled exception presents any particular problem, let us know, but generally, it shouldn't be.

Best wishes,
Ivailo
the Telerik team
0
Daní
Top achievements
Rank 1
answered on 31 Mar 2011, 10:17 AM
Hi Ivailo,

The handled exception doesn' present any problem to me, it just seems that loading a ListBox with several RichTextBox is slower than before (before Q1 2011). As I'm developping an application that replaces an old one, my view model objects may have XamlText property empty (those generated with the old application) and I need to use a PlainText property. In this scenario I created a custom behavior with a RichTextBox dependency property, when my view model XamlText is not empty, it creates a XamlDataProvider to update RichTextBox document, when XamlText is empty, it creates a TxTDataProvider to update RichTextBox document with PlainText supplied, so I'm instanciating XamlDataProvider by C# code and I can avoid the handled exception.
0
Ivailo Karamanolev
Telerik team
answered on 31 Mar 2011, 10:29 AM
Hi Daní,

We will make a review of the behavior so that if the Xaml property is empty, an empty document will be loaded in the RadRichTextBox without any exceptions being thrown. This change will be visible in the next minor release (Q1 SP1). Additionally, some new features have been added since the last release which may have certain performance impact. However, as these features get more mature, the performance generally improves as we are doing our best to make our controls as fast as possible.

Kind regards,
Ivailo
the Telerik team
0
Daní
Top achievements
Rank 1
answered on 31 Mar 2011, 10:39 AM
Thanks Ivailo,

I'm waiting expectant for uor improvements.
Tags
RichTextBox
Asked by
Philip
Top achievements
Rank 1
Answers by
hwsoderlund
Top achievements
Rank 1
Philip
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Iva Toteva
Telerik team
Daní
Top achievements
Rank 1
Share this question
or