Share via


A sample for the holidays

For my last task of the year, I answered a question about binding a ComboBox to a collection of StrokeCollections that has a custom property to indicate the name of the StrokeCollection. This was a fun little project so I decided to post it here to share. My solution was to inherit from StrokeCollection and expose the name as a property.

    public class NamedStrokeCollection : StrokeCollection

    {

        Guid nameGuid = new Guid("12345678-9012-3456-7890-123456789012");

        #region constructers that mirror StrokeCollection

        public NamedStrokeCollection()

            : base()

        {

        }

        public NamedStrokeCollection(IEnumerable<Stroke> strokes)

            : base(strokes)

        {

        }

        public NamedStrokeCollection(System.IO.Stream stream)

            : base(stream)

        {

        }

        #endregion

        public NamedStrokeCollection(string name)

            : base()

        {

            Name = name;

        }

        public NamedStrokeCollection(IEnumerable<Stroke> strokes, string name)

            : base(strokes)

        {

            Name = name;

        }

        public NamedStrokeCollection(System.IO.Stream stream, string name)

            : base(stream)

        {

            Name = name;

        }

         

        // StrokeCollection doesn't have a constructor that takes an ISF, so

        // since that's the easiest way for *me* to create a stroke collection is to pass in

        // an ISF string, I created a static method to return a NamedStrokeCollection

        // when it's passed in a string.

        static public NamedStrokeCollection GetStrokes(string strokesStr, string name)

        {

            // Create a StrokeCollection the string and add it to

            StrokeCollectionConverter converter =

          new StrokeCollectionConverter();

            StrokeCollection strokes = converter.ConvertFrom(strokesStr) as StrokeCollection;

            NamedStrokeCollection namedStrokes = new NamedStrokeCollection(strokes);

            namedStrokes.Name = name;

            return namedStrokes;

        }

        public string Name

        {

            get

            {

                if (this.ContainsPropertyData(nameGuid))

                {

                    return (string)this.GetPropertyData(nameGuid);

                }

                else

                {

                    return "";

                }

            }

            set

            {

                this.AddPropertyData(nameGuid, value);

            }

        }

    }

After that, it’s easy to create an ObservableCollection<NamedStrokeCollection> class, populate an object with NamedStokeCollections, and Bind to the object just as you would for any other object.

      <Window.Resources>

            <src:StrokesCollection x:Key="strokes"/>

      </Window.Resources>  

      <StackPanel>

            <ComboBox Name="cb1" ItemsSource="{StaticResource strokes}"

                        DisplayMemberPath="Name" SelectedIndex="0">

            </ComboBox>

            <InkCanvas Name="ic1" Strokes="{Binding ElementName=cb1, Path=SelectedItem}"/>     

      </StackPanel>

 

Pretty slick, huh? I’ve attached to sample for your convenience.

Happy Holidays and see you next year!

Carole

BindingStrokeCollectionCustomProperty.zip

Comments

  • Anonymous
    January 13, 2008
    Dude, stop misleading people. Better show how to implement RichTextBox Control in pure WPF, w/o subclassing from Windows Common Controls DLL (I mean CommCtl32.dll and RICHEDIT_CLASS)