I am kinda desperate, because yesterday it was working but today this is not working anymore, let me explain, what I am trying to do:
I want to set the DataSource
of a ComboBox
to all enum values in a specific enum. Just like that:
cmbType.DataSource = m_addViewPresenter.Types;
where Types is a BindingList
and is being initialised like that:
public BindingList<MyEnum> Types
{
get { return m_types; }
set
{
m_types = value;
OnPropertyChanged();
}
}
[ImportingConstructor]
public AddViewPresenter()
{
Types = new BindingList<MyEnum>(
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>().ToList());
}
Further more, I want to bind the current item selected to a property. Since ComboBox.SelectedItem
doesn't fire a INotifyProperty
-event, I am using ComboBox.SelectedValue
.
cmbType.Bind(m_addViewPresenter, c => c.SelectedValue, m => m.SelectedValue);
public static void Bind<TComponent, T>(
this TComponent component, T value,
Expression<Func<TComponent, object>> controlProperty,
Expression<Func<T, object>> modelProperty)
where TComponent : IBindableComponent
where T : INotifyPropertyChanged
{
var controlPropertyName = PropertyNameResolver.GetPropertyName(controlProperty);
var modelPropertyName = PropertyNameResolver.GetPropertyName(modelProperty);
component.DataBindings.Add(new Binding(controlPropertyName, value, modelPropertyName, false, DataSourceUpdateMode.OnPropertyChanged));
}
It worked fine yesterday, but something messed things up and today I am only getting an InvalidOperationException
:
Cannot set the SelectedValue in a ListControl with an empty ValueMember.
I know it's digging in the dark, but can anyone brainstorm with me and find out, what's the problem? Thanks in advance!
ComboBox.SelectedValue
requires setting 'ComboBox.ValueMember` – Ivan StoevComboBox.SelectedItem
, I don't know why do you think it will not fire property change notification, I think it does. – Ivan StoevSelectedValue
, use another class that contains a property asDisplayMember
and a property asValueMember
and shape your enum values to aList<ThatClass>
asDataSourec
, or simplye use your enum values as data source and bind toSelectedItem
as mentioned by Ivan too. – Reza Aghaei