Problema 1:C Asignación de propiedad predeterminada para la clase y el operador # =
Tengo una sencilla aplicación WinForms y quiero databind mi propiedad Person.Name a un cuadro de texto. El nombre es del tipo StringField. Originalmente, definí la propiedad Name como String. El enlace de datos funciona muy bien en tipos de valores como String. Me gustaría que la propiedad StringField.Value sea la propiedad predeterminada de StringField. Quiero ver el valor de StringField.Value en el textBox en lugar del texto "FieldApp.StringField".
Problema 2:
Me gustaría ser capaz de asignar una cadena a un Stringfield usando el operador =. Esta asignación daría como resultado el establecimiento del miembro StringField.Value.
¿Se puede lograr esto?
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");
}
}
}