Benutzer:MovGP0/WinForms/DataBinding

aus Wikipedia, der freien Enzyklopädie
< Benutzer:MovGP0‎ | WinForms
Dies ist die aktuelle Version dieser Seite, zuletzt bearbeitet am 20. Januar 2015 um 10:33 Uhr durch imported>MovGP0(77247) (→‎ComboBox).
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


ComboBox

  • 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

  • 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";
}