用于集合 DP 的 WPF TypeConversionAttribute

发布于 2024-10-19 20:47:54 字数 209 浏览 1 评论 0原文

我有一个 ObservableCollection 作为自定义控件中的依赖属性(例如 Points)。

我想像这样初始化它

<MyControl Points="1,1, 2,2"/>

如何为特定 DP 定义和创建类型转换器?

我知道有一个专门的点集合类,带有内置类型转换器,但我无法使用它。

I have an ObservableCollection as a dependency property(say Points) in a custom control.

I want to initialize it like this

<MyControl Points="1,1, 2,2"/>

How do I go about defining and creating a typeconverter for the specific DP?

I know there is a specialized points collection class with a built in typeconverter but I cannot use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-10-26 20:47:54

您可以在 CLR 属性包装器上为依赖项属性指定一个 TypeConverter。像这样:

public class MyControl : Control 
{
    [TypeConverter(typeof(MyStringToPointCollectionConverter))]
    public ObservableCollection<Point> Points {
        get { return (ObservableCollection<Point>)GetValue(Points yProperty); }
        set { SetValue(Points Property, value); }
    }
    ...
}

转换器看起来像这样:

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

        return false;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
        var stringValue = value as string;

        if (stringValue != null) {
            var result = new ObservableCollection<Point>();

            // Here goes the logic of converting the given string to the list of points

            return result;
        }

        return null;
    }
}

You can specify a TypeConverter on the CLR property wrapper for your dependency property. Like this:

public class MyControl : Control 
{
    [TypeConverter(typeof(MyStringToPointCollectionConverter))]
    public ObservableCollection<Point> Points {
        get { return (ObservableCollection<Point>)GetValue(Points yProperty); }
        set { SetValue(Points Property, value); }
    }
    ...
}

And the converter would look something like this:

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

        return false;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
        var stringValue = value as string;

        if (stringValue != null) {
            var result = new ObservableCollection<Point>();

            // Here goes the logic of converting the given string to the list of points

            return result;
        }

        return null;
    }
}
丢了幸福的猪 2024-10-26 20:47:54

好问题。有一种方法可以做到这一点 - 将点的 DP 更改为对象/字符串类型(以避免无效转换异常)并在 DP 更改事件处理程序中进行转换。最后你不会有什么损失 - DP 系统并不完全是一个类型安全的框架。

那会起作用的。我完全可以将 JSON 视为序列化数据的一种格式。

另一种方法是在可观察集合之上引入更高级别的抽象。通常这会减轻 XAML 的压力。

good question. There's a way to do that - change DP for your Points to be of object/string type (to avoid invalid cast exception) and do conversion within a DP change event handler. In the end you've got little to lose - DP system isn't exactly a type safe framework.

That will work. I can quite see JSON as a format for your serialized data.

Another approach would be to introduce a higher level of abstraction on top of your observable collection. Typically that eases the pressure on XAML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文