RadVerticalLinearGauge exporting invalid Xaml

4 posts, 0 answers
  1. Alexander
    Alexander avatar
    5 posts
    Member since:
    Dec 2015

    Posted 07 Sep Link to this post

    I am working on a project that takes some basic controls and some telerik wpf controls, places them on a canvas, and saves these for redisplay later. All of the controls are generated at runtime.

    One problem I am having is with some of the relative and scale values on all gauge controls that use the Telerik.Windows.Controls.Gauge.GaugeMeasure for storing values. instead of saving the actual value set on the property (such as 0.15*), when I save the Xaml, I get the text "Telerik.Windows.Controls.Gauge.GaugeMeasure" as the property value.

    To reproduce, place the following on a wpf window:

    <Border Name="meterBorder" BorderThickness="1" BorderBrush="Black" Background="White" Width="80" Height="300" CornerRadius="3">
            <telerik:RadVerticalLinearGauge Padding="5" Background="Transparent" BorderThickness="0" OuterBackground="Transparent" OuterBorderThickness="0" >
                <telerik:VerticalLinearScale Min="0" Max="1" RelativeY="0" RelativeHeight="0.9" VerticalAlignment="Center" Fill="#FF787878" StartWidth="0.015" EndWidth="0.015" MajorTickOffset="0.0" MinorTickOffset="0.0" MiddleTicks="1" MinorTicks="2" MajorTickStep="0.1" MajorTicks="10" LabelFormat="{}{0:F2}" LabelLocation="OverOutside" MajorTickLocation="Outside" Margin="10" FontSize="8" Foreground="Black" MajorTickStrokeThickness="1" MajorTickStroke="Black" MinorTickStrokeThickness="1" MinorTickStroke="Black">
                    <telerik:VerticalLinearScale.Indicators>
                        <telerik:BarIndicator Name="valueIndicator" HorizontalAlignment="Left" Margin="-20,0,0,0" Value="0.5" Background="#FF7171FF" StartWidth="0.2" EndWidth="0.2" StrokeThickness="0" Width="13" />
                    </telerik:VerticalLinearScale.Indicators>
                    <telerik:VerticalLinearScale.Ranges>
                        <telerik:GaugeRange x:Name="LowLowIndicator"  Background="Red" StartWidth="0.05" EndWidth="0.05" Min="0" Max="0.05" IndicatorBackground="{x:Null}" />
                        <telerik:GaugeRange x:Name="LowIndicator" Background="#FFE8FF00" StartWidth="0.05" EndWidth="0.05" Min="0.05" Max="0.15" IndicatorBackground="{x:Null}" />
                        <telerik:GaugeRange x:Name="HighIndicator" Background="#FFE8FF00" StartWidth="0.05" EndWidth="0.05" Min="0.85" Max="0.95" IndicatorBackground="{x:Null}" TickBackground="{x:Null}" StrokeThickness="0" />
                        <telerik:GaugeRange x:Name="HighHighIndicator" Background="Red" StartWidth="0.05" EndWidth="0.05" Min="0.95" Max="1" IndicatorBackground="{x:Null}" />
                    </telerik:VerticalLinearScale.Ranges>
                </telerik:VerticalLinearScale>
            </telerik:RadVerticalLinearGauge>
        </Border>

    On top of code-behind:

    using System.IO;
    using System.Xml;
    using System.Windows.Markup;

    then in window MouseDoubleClick (or some other event) place the following to save the file:

    var fileName = @"c:\temp\tester.xaml";
    using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read))
    using (var xw = new XmlTextWriter(fs, System.Text.Encoding.UTF8))
    {
     xw.Formatting = Formatting.Indented;
     xw.Indentation = 4;
     xw.IndentChar = ' ';
     
     
     XamlWriter.Save(meterBorder, xw);
    }

    throughout the saved Xaml, there will be numerous Telerik.Windows.Controls.Gauge.GaugeMeasure where values should be. Is this a bug in the Telerik controls, or is there some way for me to properly save the correct values for the telerik controls?

     

    Thanks

    Thanks

  2. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 12 Sep Link to this post

    Hi Alexander,

    The properties of type GaugeMeasure are not serialized as expected because the GaugeMeasure class was not designed to be serialized. This scenario is not taken into account and the TypeConverter used to set the property in XAML as a string, doesn't override the ConvertTo() and CanConvertTo() methods. By default the ConvertTo() method (used by the XamlWriter) will return the result from the object's value ToString() method.

    To achieve your requirement you can create a custom TypeConverter that inherits the GaugeMeasureConverter and override its CanConvertoTo() and ConvertTo() methods. Here is an example in code:
    public class MyGaugeMeasureConverter : GaugeMeasureConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;
            else
                return base.CanConvertTo(context, destinationType);
        }
     
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value != null)
            {
                var measure = (GaugeMeasure)value;
                return measure.Value.ToString();
            }           
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    //..............
    public MainWindow()
    {
        TypeDescriptor.AddAttributes(typeof(GaugeMeasure), new TypeConverterAttribute(typeof(MyGaugeMeasureConverter)));
        InitializeComponent();
    }

    I hope this helps.

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alexander
    Alexander avatar
    5 posts
    Member since:
    Dec 2015

    Posted 12 Sep in reply to Martin Link to this post

    No change in the Xaml getting saved after implementing this custom converter, although while debugging, the ConvertTo method is getting called several times and measure.Value.ToString() is getting returned. It's just that the output is not getting changed.

    Any more thoughts?

  5. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 13 Sep Link to this post

    Hello Alexander,

    On my side some of the offsets are saved properly and others are not. It seems that the custom type converter doesn't kick-in for the attached properties of type GaugeMeasure.

    Another approach is to manually serialize the gauge or replace the MeasureGauge values after the Xaml string is saved.

    Additionally, I logged a feature request in our feedback portal to implement the ConvertTo() method of the type converter. You can follow the item's status there and vote for an implementation. I also updated your Telerik points for point us to this limitation of the GaugeMeasure class.

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top