2009-01-11 56 views
6

问题1:C#类和运营商分配默认属性=

我有一个简单的WinForms应用程序,我想我的Person.Name属性数据绑定到一个文本框。名称的类型是StringField。我最初将Name属性定义为String。数据绑定对值类型如String很有效。我想StringField.Value属性是StringField的默认属性。我希望在文本框中看到StringField.Value的值,而不是文本“FieldApp.StringField”。

问题2:

我想能够使用操作者的字符串指定给一个StringField =。此分配会导致StringField.Value成员被设置。

这可以完成吗?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace FieldApp 
{ 
    public class StringField 
    { 
     public string Value { get; set; }  
    } 

    public class Person 
    { 

     //private String _Name; 
     //public String Name 
     //{ 
     // get { return _Name; } 
     // set { _Name = value; } 
     //} 

     //public Person(string name) 
     //{ 
     // Name = name; 
     //} 

     private StringField _Name; 
     public StringField Name 
     { 
      get { return _Name; } 
      set { _Name = value; } 
     } 

     public Person(string name) 
     { 
      Name = new StringField(); 
      Name.Value = name; 
     } 
    } 

    public partial class FieldAppForm : Form 
    { 
     Person person = new Person("steve"); 

     public FieldAppForm() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //our form contains a button1 and textBox1 

      //this compiles 
      person.Name.Value = "steve"; 

      //this does not. Is there anyway to accomplish this? 
      person.Name = "steve"; 

      //steve appears in the textbox 
      textBox1.DataBindings.Add("Text", person, "Name.Value"); 

      //FieldApp.StringField appears in the textbox 
      textBox1.DataBindings.Add("Text", person, "Name"); 
     } 
    } 
} 

回答

10

您可以创建一个implisit运算符重载。然后你可以从这样的字符串创建Stringfield:

StringField field = "value of new object"; 
string value=(string)field; 

知道这会创建一个新的StringField对象。我不会非常建议你这样做。

[System.Diagnostics.DebuggerDisplay("{Value}")] 
public class StringField 
{ 
    public string Value { get; set; } 
    public static implicit operator StringField(string s) 
    { 
     return new StringField { Value = s }; 
    } 

    public static explicit operator string(StringField f) 
    { 
     return f.Value; 
    } 
    public override string ToString() 
    { 
     return Value; 
    } 
} 
0

您可以通过将Name属性映射到您的类的内部Name.Value字段来修饰StringField。

,所以你可以定义Name属性是这样的:

string Name 
{ 
    get { return _name.Value; } 
    set { _name.Value = value; } 
} 

这里_name是你StringField变量。

0

赋值运算符不能在C#中被覆盖。然而,你可能有一个属性做类型转换为你揭露一个类

2

重数据绑定,为一些约束性指标(PropertyGridDataGridView等),您可以用TypeConverter做到这一点(见下文)。不幸的是,这似乎并没有与TextBox工作,所以我想你最好的选择是简单地增加一个垫片属性(如已经建议):

string NameString 
{ 
    get { return Name.Value; } 
    set { Name.Value = value; } // or new blah... 
} 

(并绑定到NameString

在过去,我已经使用定制的PropertyDescriptor实现来支持这一点,但它不值得为此只是

反正TypeConverter例子(与PropertyGridDataGridView作品):

[TypeConverter(typeof(StringFieldConverter))] 
public class StringField 
{ 
    public StringField() : this("") { } 
    public StringField(string value) { Value = value; } 
    public string Value { get; private set; } 
} 

class StringFieldConverter : TypeConverter 
{ 
    public override bool CanConvertFrom(
     ITypeDescriptorContext context, Type sourceType) 
    { 
     return sourceType == typeof(string) 
      || base.CanConvertFrom(context, sourceType); 
    } 
    public override object ConvertFrom(
     ITypeDescriptorContext context, 
     System.Globalization.CultureInfo culture, 
     object value) 
    { 
     string s = value as string; 
     if (s != null) return new StringField(s); 
     return base.ConvertFrom(context, culture, value); 
    } 
    public override bool CanConvertTo(
     ITypeDescriptorContext context, Type destinationType) 
    { 
     return destinationType == typeof(string) 
      || base.CanConvertTo(context, destinationType); 
    } 
    public override object ConvertTo(
     ITypeDescriptorContext context, 
     System.Globalization.CultureInfo culture, 
     object value, Type destinationType) 
    { 
     if (destinationType == typeof(string) && value != null 
      && value is StringField) 
     { 
      return ((StringField)value).Value; 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 
} 
1

您可以通过提供转换运算符来实现赋值。鉴于您的课程的性质,您还应该覆盖Object方法:

public class StringField { 
    public string Value { get; set; } 
    public static implicit operator StringField(string value) { 
    StringField sf = new StringField(); 
    sf.Value = value; 
    return sf; 
    } 
    public override string ToString() { 
    return Value; 
    } 
    public override bool Equals(object obj) { 
    if (obj == null || !(obj is StringField)) return false; 
    return 0 == string.Compare(Value, (obj as StringField).Value); 
    } 
    public override int GetHashCode() { 
    return Value.GetHashCode(); 
    } 
}