开发者

WPF Visual Studio Designer and Expression Blend are not respecting TypeConverter on DependencyProperty

I have added the dependency property MyList to a wpf textbox. The dependency property is of type List<string>. In order to make things easier in xaml I have defined a converter so that I can have following syntax:

<Grid>
    <controls:MyTextBox x:Name="Hello" MyList="One,Two" Text="Hello" />
</Grid>

In Visual Studio I can't edit the property at all and in Expression Blend I can type in the string but it generates the following xaml:

<controls:MyTextBox x:Name="Hello" Text="Hello" >
 <controls:MyTextBox.MyList>
  <System_Collections_Generic:List`1 Capacity="2">
   <System:String>One</System:String>
   <System:String>Two</System:String>
  </System_Collections_Generic:List`1>
 </controls:MyTextBox.MyList>
</controls:MyTextBox>

Any ideas how I can just edit this property as a string in both Visual Studio and Blend??

public class MyTextBox : TextBox
{
    [TypeConverter(typeof(MyConverter))]
    public List<string> MyList
    {
        get { return (List<string>)GetValue(MyListProperty); }
        set { SetValue(MyListProperty, value); }
    }

    public开发者_JAVA百科 static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(List<string>), typeof(MyTextBox), new FrameworkPropertyMetadata(new List<string> { "one" }));
}


public class MyConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if(sourceType == typeof(string))
            return true;
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if(value is string)
            return new List<string>(((string)value).Split(','));    

        return base.ConvertFrom(context, culture, value);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if(destinationType == typeof(string))
            return true;
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if(destinationType == typeof(string))
        {
            var ret = string.Empty; 
            var s = ret;
            ((List<string>)value).ForEach(v => s += s + v + ",");
            ret = ret.Substring(0, ret.Length - 1);

            return ret;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }
}


there is no possibility for doing this with generics, both VS and Blend designers will generate the collection information with those tags while doing design-time serialization. One work around is to create your own data type for MyList instead of List. :(

Or

You need to keep the MyList as a String property then, parse subsequent strings and store them into a List.

Or

One more possible solution. [if you know the values of list earlier]

Instead of using a List<string> make it an enum with Flags. So, that you can get the expected output syntax in both VS Designer and Blend without those junk codes.

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜