views:

92

answers:

1

Hello everybody,

I'm a starter at WPF, now i would like to make a WPF userControl library which include a Rating bar userControl. All the steps of creating the rating Bar has been done, however i would like to add a property RatingValue:

public static readonly DependencyProperty RatingValueProperty =
            DependencyProperty.Register("RatingValue", typeof(int), typeof(RatingControl),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(RatingValueChanged)));

public int RatingValue
        {
            get { return (int)GetValue(RatingValueProperty); }
            set
            {               
                SetValue(RatingValueProperty, value);                
            }
        }

private static void RatingValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
         //... change the rating value
        }

that the user of my UserControl can modify by a value from 0 to 5 that are shown in a dropdown list (combo box) in the Properties windows (as some exist properties of Usercontrols like Visibility, windows style, background ...)

How can i do? Thank you very much in advance,

Viet

A: 
  1. Create a class derived from TypeConverter.
  2. Override GetStandardValues and GetStandardValuesSupported (and optionally GetStandardValuesExclusive).
  3. From GetStandardValues, return a collection containing the values you want to appear in the combo box.
  4. Apply TypeConverterAttribute to the RatingValue property, specifying the type of your type converter.

Alternatively, depending on the semantics of RatingValue, you might consider making it an enum. This feels a bit weird because the values are numeric -- but it would have the advantage of constraining the values at a type level, and it would automatically give you a combo box with no need for you to implement a type converter.

itowlson
Thank you for your quick answer, however i'm sorry i didn't quite get to them, would you mind giving me an example for the solutions? especially the "enum" one, seems interesting and simpler :)Thank you very much.
Viet
The enum approach just involved creating an enum type (`public enum Rating { ... }`) and using that as the RatingValue property type instead of `int`. For the type converter approach, see http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx (scroll down to the section entitled "Type Converters That Provide a List of Standard Values to a Properties Window").
itowlson
Thank you very much :)
Viet
By the way, is there a way that we can create an enum with the elements' name as number like this: public enum Rating { 1, 2, 3 ...} instead of { one = 1, two, three ...} (because they always demand a name ...)
Viet
No, but you can create "friendly names" for your enum values, e.g. using a value converter. See http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx for an example.
itowlson
Thank you very much for your help :)
Viet