This project has moved and is read-only. For the latest updates, please go here.

AutoCAD Custom UpDown

Feb 15, 2016 at 9:30 AM
Edited Feb 23, 2016 at 9:40 AM
Based on Extended WPF Toolkit I want to create my own CommonNumericUpDown<Station> for AutoCAD Stations along Alignments. This works pretty fine in a StandAlone-WPF-App. If I netload it in AutoCAD from anywhere on disk I get a XamlParseException "The method or operation is not implemented.", but placing my dll in AutoCAD-install-directory it also works fine. Also creating my own CommonNumericUpDown<Double> works pretty fine. The error also only appears as soon as on setting one of UpDownBase's properties. Where is AutoCAD cheating me?

Here is my Code. To simplify as much as possible I extended UpDownBase<T>:
using System;
using System.Windows;

using Xceed.Wpf.Toolkit.Primitives;

using Autodesk.AutoCAD.Runtime;

namespace My.AutoCAD.UITest
{
    class UpDownBaseDoubleUpDown : UpDownBase<double>
    {
        protected override double ConvertTextToValue(string text)
        {
            return double.Parse(text);
        }

        protected override string ConvertValueToText()
        {
            return Value.ToString();
        }

        protected override void OnDecrement()
        {
            Value -= 1;
        }

        protected override void OnIncrement()
        {
            Value += 1;
        }

        protected override void SetValidSpinDirection() { }

        static UpDownBaseDoubleUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UpDownBaseDoubleUpDown), new FrameworkPropertyMetadata(typeof(UpDownBaseDoubleUpDown)));
        }
    }

    public struct Station : IFormattable, IComparable<Station>
    {
        public double Value { get; set; }

        public Station(double value) { Value = value; }

        public int CompareTo(Station other)
        {
            return Value.CompareTo(other.Value);
        }

        public static Station Parse(string s) { return new Station(double.Parse(s)); }

        public override string ToString() { return Value.ToString(); }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Value.ToString(format, formatProvider);
        }
    }

    public class UpDownBaseStationUpDown : UpDownBase<Station>
    {
        protected override Station ConvertTextToValue(string text)
        {
            return Station.Parse(text);
        }

        protected override string ConvertValueToText()
        {
            return Value.ToString();
        }

        protected override void OnDecrement()
        {
            Value = new Station(Value.Value - 1);
        }

        protected override void OnIncrement()
        {
            Value = new Station(Value.Value + 1);
        }

        protected override void SetValidSpinDirection() { }

        static UpDownBaseStationUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UpDownBaseStationUpDown), new FrameworkPropertyMetadata(typeof(UpDownBaseStationUpDown)));
        }
    }

    /// <summary>Interaction logic for StationUpDownTest.xaml</summary>
    public partial class StationUpDownTest : Window
    {
        public StationUpDownTest()
        {
            InitializeComponent();
        }
    }

    public class Commands
    {
        [CommandMethod("TestMgdStationUpDown")]
        public void TestMgdStationUpDown()
        {
            var dlg = new StationUpDownTest();
            Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowModalWindow(dlg);
        }
    }
}
Generic.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                    xmlns:local="clr-namespace:My.AutoCAD.UITest"
                    xmlns:core="clr-namespace:Xceed.Wpf.Toolkit.Core;assembly=Xceed.Wpf.Toolkit"
                    >
    <ResourceDictionary.MergedDictionaries>
        <core:VersionResourceDictionary AssemblyName="Xceed.Wpf.Toolkit" SourcePath="NumericUpDown/Themes/Generic.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="local:UpDownBaseStationUpDown" BasedOn="{StaticResource NumericUpDown}" />

    <Style TargetType="local:UpDownBaseDoubleUpDown" BasedOn="{StaticResource NumericUpDown}" />

</ResourceDictionary>
StationUpDownTest.xaml:
<Window
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:My.AutoCAD.UITest"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" x:Class="My.AutoCAD.UITest.StationUpDownTest"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <!-- doesn't work -->
        <local:UpDownBaseStationUpDown HorizontalAlignment="Left" Margin="55,106,0,0" VerticalAlignment="Top" Width="136" Height="47"
                                       AllowSpin="False"
                                       />

        <!-- works -->
        <local:UpDownBaseDoubleUpDown HorizontalAlignment="Left" Margin="55,106,0,0" VerticalAlignment="Top" Width="136" Height="47" 
                                    AllowSpin="False"
                                    />
    </Grid>
</Window>
StackOverflow: http://stackoverflow.com/questions/35316653/autocad-custom-extended-wpf-updown/35333732