I have a Combobox and I want to edit a boolean value.
<dxe:ComboBoxEdit ItemsSource="{Binding EnumItemsSource}"
DisplayMember="Name"
ValueMember="Id"
IsTextEditable="False"
EditValue="{Binding TargetValue, UpdateSourceTrigger=PropertyChanged}"/>
My ViewModel:
/// <summary>
/// Contains the ItemsSource for Enums
/// </summary>
public List<EnumItemObject> EnumItemsSource
{
get { return _enumItemsSource; }
set
{
_enumItemsSource = value;
OnPropertyChanged();
}
}
public class EnumItemObject
{
public int Id {get;set;}
public string Name {get;set;}
}
And I prepare the data for the Combobox ItemsSource that:
/// <summary>
/// Sets the value to the properties for the BitTemplate view. (similar with EnumTemplate)
/// </summary>
/// <param name="propertyInfo">a boolean property</param>
private void PrepareDataForBitTemplate(PropertyInfo propertyInfo)
{
TargetValue = (int)propertyInfo.GetValue(_firstSelectedItem);
EnumItemsSource = new List<EnumItemObject>();
EnumItemsSource.Add(new EnumItemObject() { Id = 0, Name = "Nein" });
EnumItemsSource.Add(new EnumItemObject() { Id = 1, Name = "Ja" });
}
Is it the approach correct? Is there any easier solution?
UPDATE: Extract from my ViewModel code (you remember EnumItemsSource is ItemsSource for Combobox and TargetValue the Combobox selected item):
private void LookUpViewData()
{
var propertyInfo = _firstSelectedItem.GetType().GetProperty(TargetFieldDescription.fdBigViewColumnName);
if ((propertyInfo != null) && (propertyInfo.GetValue(_firstSelectedItem) != null))
{
if ((int) FieldDataType.ENum == TargetFieldDescription.fdDataType)
PrepareDataForEnumTemplate(propertyInfo);
if ((int) FieldDataType.Bit == TargetFieldDescription.fdDataType)
PrepareDataForBitTemplate(propertyInfo); // like EnumTemplate
else if ((int) FieldDataType.Time == TargetFieldDescription.fdDataType)
PrepareDataForTimeTemplate(propertyInfo);
else
PrepareDataForDefaultTemplate(propertyInfo);
}
else
TargetValue = String.Empty;
}
private void PrepareDataForEnumTemplate(PropertyInfo propertyInfo)
{
TargetValue = (int)propertyInfo.GetValue(_firstSelectedItem);
if (_targetFieldDescription.fdEnumSource != null)
{
switch (_targetFieldDescription.fdEnumSource)
{
case "Station":
EnumItemsSource = ConvertListToEnumItemObjectList(_targetFieldDescription.fdEnumSource);
break;
default:
EnumItemsSource = EnumUtil.ConvertEnumToEnumItemObjectList(_targetFieldDescription.fdEnumSource);
break;
}
}
}
private void PrepareDataForBitTemplate(PropertyInfo propertyInfo)
{
var value = propertyInfo.GetValue(_firstSelectedItem);
bool bValue;
if (value == null)
TargetValue = 0;
else if (bool.TryParse(value.ToString(), out bValue))
TargetValue = (bValue) ? 1 : 0;
else
TargetValue = 0;
//TargetValue = (int)propertyInfo.GetValue(_firstSelectedItem);
var bitItems = new List<EnumItemObject>();
bitItems.Add(new EnumItemObject { Id = 0, Name = "Nein" });
bitItems.Add(new EnumItemObject { Id = 1, Name = "Ja" });
EnumItemsSource = bitItems;
}
So I want to load three possible kind of data in my Combobox. Enums, List of Objects (with name and id) and booleans (Yes/No List).