一个人怎么数据绑定的自定义类型TextBox.Text? [英] How does one databind a custom type to TextBox.Text?

查看:165
本文介绍了一个人怎么数据绑定的自定义类型TextBox.Text?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的C#类型一样(只是一个例子):

I have a custom c# type like (just an example):

public class MyVector
{ 
   public double X {get; set;} 
   public double Y {get; set;} 
   public double Z {get; set;} 
   //...
}

和我想它数据绑定到TextBox.Text:

And I want it to databind to TextBox.Text:

TextBox textBox;
public MyVector MyVectorProperty { get; set;}
//...
textBox.DataBindings.Add("Text", this, "MyVectorProperty");



基本上我需要转换,并从我的自定义值类型的字符串。在文本框中,我想是这样的X,Y,Z可编辑更新矢量类型。我认为我可以通过添加一个的TypeConverter 派生类这样做的:

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

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

    public override object ConvertFrom(ITypeDescriptorContext context, 
                                       System.Globalization.CultureInfo culture,
                                       object value)
    {
        if (value is string)
        {
            MyVector MyVector;
            //Parse MyVector from value
            return MyVector;
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context,
                                     System.Globalization.CultureInfo culture, 
                                     object value, 
                                     Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            string s;
            //serialize value to string s
            return s;
        }
        //...
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

和它与我的结构关联:

[TypeConverter(typeof(MyVectorConverter))]
public class MyVector { //... }

这似乎完成成功的一半。我可以看到 MyVectorConverter 获取调用到,但有什么不妥。这就是所谓,看它是否知道如何转换为字符串,则它被称为转换为字符串。然而,它永远不会查询,看它是否可以从字符串转换也不实际做转换。此外,在文本编辑之后,旧的值被立即更换(另一个CanConvertTo和序列的ConvertTo,恢复旧值)。最终的结果是,应用后的文本框中新键入的条目立即恢复。

This appears to complete half of the battle. I can see MyVectorConverter getting called into, but something is amiss. It is called to see if it knows how to convert to string, then it is called to convert to string. However, it is never queried to see if it can convert FROM string nor to actually do the conversion. Furthermore, right after an edit in the textbox, the old value is immediately replaced (another CanConvertTo and ConvertTo sequence, restoring the old value). The end result is that the newly typed entry in the text box is reverted immediately after it is applied.

我觉得如果有只是简单的东西。在那里?这是整个项目/方法注定要失败?没有任何人尝试这种疯狂?怎样才能双向绑定一个自定义的,多部分类型到基于字符串的控制

I feel as if there is just something simple missing. Is there? Is this entire project/approach doomed to failure? Does anyone else attempt such madness? How does one bi-directionally bind a custom, multipart type to a string-based control?

解决方案:的奇怪的是,所有需要的是对于格式化的绑定对象上启用。 (感谢,​​乔恩斯基特):

Solution: Bizarrely, all that is needed is for the "formatting" to be enabled on the Binding object. (thanks, Jon Skeet):

textBox.DataBindings.Add("Text", this, "MyVectorProperty"); //FAILS
textBox.DataBindings.Add("Text", this, "MyVectorProperty", true); //WORKS!



奇怪的是,所有我MSDN提到有关此参数(formattingEnabled)是:

Oddly, all that my MSDN mentions about this parameter (formattingEnabled) is:

真格式化显示的数据;否则为false

"true to format the displayed data; otherwise, false"

它没有提及它是一个要求的数据从控制(在这些条件下)回来。

It mentions nothing about it being a requirement for the data to come back from the control (under these conditions).

推荐答案

< STRONG>心动不如行动!

设置的 Binding.FormattingEnabled 属性设置为true。这似乎使这一切的工作。
你可以过载到 ControlBindingsCollection.Add 方法,需要一个布尔参数结尾。
很奇怪的是,它的工作的一种方式而不是其他之前,但肯定我的测试应用程序现在工作...

Set the Binding.FormattingEnabled property to true. This seems to make it all work. You can do this with an overload to the ControlBindingsCollection.Add method which takes a Boolean parameter at the end. It's odd that it worked one way but not the other before, but certainly my test app now works...

(下旧的答案)

我也不会感到惊讶,如果事实,你有一个结构,而不是一类是这里重要的 - 以及你正在使用的不是域的方式。属性

I wouldn't be at all surprised if the fact that you're got a struct instead of a class was important here - as well as the way you're using fields instead of properties.

尝试使用autoimplemented属性,而不是一个类:

Try with a class using autoimplemented properties instead:

public class MyClass
{ 
   public int IntPart { get; set; } 
   public string StringPart { get; set; }
   //...
}

这可能不是。这个问题,但使用公共领域的一个可变结构的根找麻烦IMO

This may well not be the root of the problem, but using a mutable struct with public fields is just asking for trouble IMO.

编辑:在评论中提到的,我现在已经有一个例子启动和运行。该Binding.Parse被提出了与正确的价值。我们找出为什么TypeConverter的不被称为...

As mentioned in the comments, I've now got an example up and running. The Binding.Parse is being raised with the right value. Now to find out why the TypeConverter isn't being called...

编辑:我发现了一个的有用的文章其中详细描述了具有约束力。这似乎表明,类型转换器只用于转换到另一种类型 - 所以你需要的类型转换器字符串来知道如何转换到自定义类型。这似乎很奇怪,我,无可否认,但也有其他两个选项:

I've found a useful article which describes binding in more detail. It seems to suggest that the type converter is only used to convert "to" another type - so you'd need the type converter for string to know how to convert to the custom type. This seems pretty strange to me, admittedly, but there are two other options:


  • 使用格式化和分析结合来完成转换的事件

  • 请类型实现IConvertible

无论以同样的方式将这些上诉,但他们的可能的够你一种解决方法。我敢肯定有一种方式来获得这种使用类型转换器的工作,但我炸飞,如果我当时可以看到它。

Neither of these appeal in quite the same way, but they may be enough of a workaround for you. I'm sure there's a way to get this to work using TypeConverters, but I'm blowed if I can see it at the moment.

这篇关于一个人怎么数据绑定的自定义类型TextBox.Text?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆