Benutzer:MovGP0/WinForms/DataBinding

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


  • MyProperty can be of any enumerable type. Usually IEnumerable<T>, BindingList<T>, or ObservableList<T>.
MyDropDown.DataBindings.Add(@"SelectedValue", ViewModel, @"MyProperty", false, DataSourceUpdateMode.OnPropertyChanged);
MyDropDown.DataBindings.Add(@"Enabled", ViewModel, @"IsMyPropertyEnabled", false, DataSourceUpdateMode.OnPropertyChanged);

BindDropDownDataSource();

// observe changes and rebind when something changes
var subscription = ViewModel
    .Where(args => args.PropertyName == "MyProperty")
    .Subscribe(args => BindDropDownDataSource());

_subscriptions.Add(subscription); // remember to dispose subscriptions

 

private void BindDropDownDataSource()
{
    MyDropDown.DataSource = ViewModel.MyProperty;
    MyDropDown.DisplayMember = @"DisplayName";
    MyDropDown.ValueMember = @"Id";
}

DevExpress LookupEdit

[Bearbeiten | Quelltext bearbeiten]
  • MyProperty must be of type BindingList<T>.
MyDropDown.DataBindings.Add(@"EditValue", ViewModel, @"MyProperty", false, DataSourceUpdateMode.OnPropertyChanged); // EditValue instead of SelectedValue
MyDropDown.DataBindings.Add(@"Enabled", ViewModel, @"IsMyPropertyEnabled", false, DataSourceUpdateMode.OnPropertyChanged);

BindDropDownDataSource();

// observe changes and rebind when something changes
var subscription = ViewModel
    .Where(args => args.PropertyName == "MyProperty")
    .Subscribe(args => BindDropDownDataSource());

_subscriptions.Add(subscription); // remember to dispose subscriptions

 

private void BindDropDownDataSource()
{
    // use the Properties-Property
    MyDropDown.Properties.DataSource = ViewModel.MyProperty;
    MyDropDown.Properties.DisplayMember = @"DisplayName";
    MyDropDown.Properties.ValueMember = @"Id";
}