Problem 1:
I have a simple winforms app and I want to DataBind my Person.Name property to a textbox. Name is of type StringField. I originally defined the Name property as String. The data binding works great on value types such as String. I would like the StringField.Value property to be the default property of StringField. I want to see the value of StringField.Value in the textBox rather than the text "FieldApp.StringField".
Problem 2:
I would like to be able to assign a string to a StringField using operator =. This assignment would result in the StringField.Value member being set.
Can this be accomplished?
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");
}
}
}
You could create an implicit operator overload. Then you can create StringField
from strings like this:
StringField field = "value of new object";
string value=(string)field;
Know that this creates a new StringField
object. I wouldn't necessarily advice you to do this.
[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;
}
}
Re data-binding, for some binding targets ( PropertyGrid
, DataGridView
, etc), you can do this with a TypeConverter
(see below). Unfortunately, this doesn't seem to work with TextBox
, so I think your best option is to simply add a shim property (as already suggested):
string NameString
{
get { return Name.Value; }
set { Name.Value = value; } // or new blah...
}
(and bind to NameString
)
In the past, I have used custom PropertyDescriptor
implementations to side-step this, but it isn't worth it just for this.
Anyway, a TypeConverter
example (works with PropertyGrid
and DataGridView
):
[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);
}
}
You can implement assignment by providing a conversion operator. Given the nature of your class, you should also override the Object methods:
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();
}
}
You can hade the StringField by mapping the Name property to the Name.Value field internally in your class.
so you can define the Name property like this:
string Name
{
get { return _name.Value; }
set { _name.Value = value; }
}
Here _name is your StringField variable.
The assignment operator cannot be overridden in C#. You could however have a property to do the type conversion for you and expose that one the class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.