I am trying to implement a drop-down property in a custom component, and I used This SO Answer and this answer as a guide.
So far I managed to get it working, with predefined items in the drop-down list.
But I still need to figure out how to alter the items in the drop-down list ?
This is the code I have so far (build from the link mentioned above)
[TypeConverter(typeof(TableNameConverter))]
public TableName gttTableName
{ get; set; }
...
public class TableName
{
public string Name { get; set; }
public override string ToString()
{
return $"{Name}";
}
}
public class TableNameService
{
List<TableName> list = new List<TableName>() {
new TableName() {Name = "naam 1" },
new TableName() {Name = "naam 2" },
};
public IEnumerable<TableName> GetAll()
{
return list;
}
}
public class TableNameConverter : TypeConverter
{
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
var svc = new TableNameService();
return new StandardValuesCollection(svc.GetAll().ToList());
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value != null && value.GetType() == typeof(string))
{
var v = $"{value}";
//var id = int.Parse(v.Split('-')[0].Trim());
var name = v.ToString();
var svc = new TableNameService();
//return svc.GetAll().Where(x => x.Id == id).FirstOrDefault();
return svc.GetAll().Where(x => x.Name == name).FirstOrDefault();
}
return base.ConvertFrom(context, culture, value);
}
}
This looks like this in VS property window
Now for the problem, when a certain property changes then the items in the drop-down property must change. This should be done in the method UpdateTableNames
(code is below).
In other words, in the setter of another property the items naam 1
, naam 2
can change to a complete new set, with more or less items and with different values.
What I can not figure out yet is how can I alter these items ?
Let me explain the situation.
- A user drops the custom component on a form
- when he looks at the property gttTableName it will now show
naam 1
andnaam 2
- Now the user changes another property (gttDataModule) and in the setter of this property the items of the gttTableName property can change.
- So if he looks again at the property gttTableName it should now show a complete other list of values.
The code for the property gttDataModule is this
public gttDataModule gttDataModule
{
get { return _gttDataModule; }
set
{
_gttDataModule = value;
UpdateTableNames();
}
}
private void UpdateTableNames()
{
List<string> tableNames = new List<string>();
if (_gttDataModule != null)
{
foreach (gttDataTable table in _gttDataModule.gttDataTables)
{
tableNames.Add(table.Table.TableName);
}
}
// at this point the list tableNames will be populated with values.
// What I need is to replace the list in TableNameService from 'naam 1', 'naam 2'
// to the values in this list.
// so they won't be 'naam 1' and 'naam 2' anymore
// It could be more or less items or even none
// They could have different values
// for example the list could be 'tblBox', 'tblUser', tblClient', tblOrders'
// or it could be empty
// or it could be 'vwCars', 'tblSettings'
}
How can I change the items in the list
of the TableNameService
?