Problem:
I am parsing an XML , all that part works perfectly. One of the items is a string of names, separated by commas. What I want is a control that can bind to a class to set and unset the string, based on user selections.
Tests
The serialization and deserialization is automatic and works perfectly, but it obviously leaves me the result in a string. The class that serializes the XML has the interface INotifyPropertyChanged
, working for all properties.
I create another class, which is supposed to be the one I'm going to bind to my control:
public class ListaMarcas: System.ComponentModel.INotifyPropertyChanged
{
private bool bMarcado;
private string sTexto;
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler handler = this.PropertyChanged;
if ((handler != null))
{
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public bool Marcado
{
get
{
return bMarcado;
}
set
{
if ((bMarcado.Equals(value) != true))
{
bMarcado = value;
this.OnPropertyChanged("Marcado");
}
}
}
public string texto
{
get
{
return sTexto;
}
set
{
if ((sTexto.Equals(value) != true))
{
sTexto = value;
this.OnPropertyChanged("texto");
}
}
}
}
This class should fill an object ObservableCollection
for the roundtrip to work. I do that on the class that serializes:
private ObservableCollection<ListaMarcas> pListaSistemas;
[XmlIgnore]
public ObservableCollection<ListaMarcas> ListaMarcas
{
get
{
return pListaSistemas;
}
set
{
if ((pListaSistemas != null))
{
if ((pListaSistemas.Equals(value) != true))
{
pListaSistemas = value;
this.OnPropertyChanged("select");
}
}
else
{
pListaSistemas = value;
this.OnPropertyChanged("select");
}
}
}
How do I make the string property fill itself, and be transformed into a valid list?
public consultaRestriccion restriccion
{
get
{
return this.restriccionField;
}
set
{
if ((this.restriccionField != null))
{
if ((restriccionField.Equals(value) != true))
{
this.restriccionField = value;
this.OnPropertyChanged("restriccion");
}
}
else
{
this.restriccionField = value;
this.OnPropertyChanged("restriccion");
}
}
}
Note that the code is automatically written from XSD2Code , so it is in a partial class.
The string comes in the format: "dato1,dato2,dato3"
, which makes it easy to transform it into a list by doing split
. Where should I put that split
so that all objects are automatically kept up to date?
Edition
The view displays that string, as a list of checkboxes that the user can select or not, and the selected ones have to be the comma-separated string that is sent back to the XML.
To solve this, what I did was create an event in the class
ListaMarcas
that is executed every time a mark changes.The call
CambioUnCheck?.Invoke();
is a simplification to check thatCambioUnCheck
it is not null before doing theinvoke
.The object
pListaSistemas
stays the same, but when I fill it with objects of typeListaMarcas
, I also declare a function that is going to be the receiver of the event:And when filling the list I do something like this:
The only problem is that on the first load, the event is executed every time I load the list for the first time. To avoid problems with that, add a variable in the class:
and in the event receiver add:
In this way, in the first load I leave AvoidEvent to true, and then I leave it to false so the event is executed on each change.
What the function
HacerCambioDeCheck
does is reload the string in the model, with each change of a checkbox.It might help to add a new attribute in the MarkList class with a method that transforms the string stext attribute into a list.