Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF Visual Studio Designer和Expression Blend在DependencyProperty上不支持TypeConverter_Wpf_Wpf Controls_Expression Blend - Fatal编程技术网

WPF Visual Studio Designer和Expression Blend在DependencyProperty上不支持TypeConverter

WPF Visual Studio Designer和Expression Blend在DependencyProperty上不支持TypeConverter,wpf,wpf-controls,expression-blend,Wpf,Wpf Controls,Expression Blend,我已经将依赖属性MyList添加到wpf文本框中。依赖项属性的类型为List。为了简化xaml中的工作,我定义了一个转换器,以便使用以下语法: <Grid> <controls:MyTextBox x:Name="Hello" MyList="One,Two" Text="Hello" /> </Grid> 在Visual Studio中,我根本无法编辑属性,在Expression Blend中,我可以键入字符串,但它会生成以下xaml: <

我已经将依赖属性MyList添加到wpf文本框中。依赖项属性的类型为List。为了简化xaml中的工作,我定义了一个转换器,以便使用以下语法:

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

在Visual Studio中,我根本无法编辑属性,在Expression Blend中,我可以键入字符串,但它会生成以下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>

一个
两个
有没有办法在Visual Studio和Blend中将此属性作为字符串进行编辑??

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

    public 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);
    }
}
公共类MyTextBox:TextBox
{
[TypeConverter(typeof(MyConverter))]
公共列表列表
{
获取{return(List)GetValue(MyListProperty);}
set{SetValue(MyListProperty,value);}
}
public static readonly dependencProperty MyListProperty=dependencProperty.Register(“MyList”、typeof(List)、typeof(MyTextBox)、new FrameworkPropertyMetadata(new List{“one”);
}
公共类MyConverter:TypeConverter
{
公共覆盖布尔CanConvertFrom(ITypeScriptorContext上下文,类型sourceType)
{
if(sourceType==typeof(string))
返回true;
返回base.CanConvertFrom(上下文,sourceType);
}
公共重写对象ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值)
{
if(值为字符串)
返回新列表(((字符串)值).Split(',');
返回base.ConvertFrom(上下文、区域性、值);
}
公共覆盖布尔CanConvertTo(ITypeDescriptorContext上下文,类型destinationType)
{
if(destinationType==typeof(string))
返回true;
返回base.CanConvertTo(上下文,destinationType);
}
公共重写对象转换为(ITypeDescriptorContext上下文、CultureInfo区域性、对象值、类型destinationType)
{
if(destinationType==typeof(string))
{
var ret=string.Empty;
var s=ret;
((列表)值);
ret=ret.Substring(0,ret.Length-1);
返回ret;
}
返回base.ConvertTo(上下文、区域性、值、destinationType);
}
}

使用泛型不可能做到这一点,VS和Blend设计器在进行设计时序列化时都将使用这些标记生成集合信息。解决方法之一是为MyList而不是List创建自己的数据类型:(

然后,您需要将MyList保持为字符串属性,解析后续字符串并将其存储到列表中

还有一个可能的解决方案。[如果您之前知道列表的值]

不要使用
列表
而是使其成为带有标志的枚举。这样,您就可以在VS Designer和Blend中获得预期的输出语法,而不需要那些垃圾代码