I have a DevExpress CheckedComboBoxEdit on my form.
In designer i had binded this control with BindingSource.
In BindingSource i have objects
public class MyClass
{
public int Id {set;get;}
public string Name {set;get;}
}
I set DisplayMember as Name and set ValueMember as empty so i expect that i got whole object from EditValue.
Now i want to bind this control with my view model
public class MyViewModel{
***
public List<MyClass> TestList{set;get;}
***
public MyViewModel(){
TestList = new List<MyClass>();
}
}
After i run app with this form. Select values in control.
But after i see that MyViewModel.TestList is empty.
And CheckedComboBoxEdit.EditValue contains list of MyClass.Name what is i setted as DisplayMember.
It is possible to bind CheckedComboBoxEdit to property and make taht EditValue retunr whole object?
Related
Currently building a WinForms data collection tool using C# and have been able to implement the binding of the values of my underling object/entity's properties to controls on the my form, e.g. TextBoxes, MaskedTextBoxes and Checkboxes to properties on my domain model class (e.g. Person class). However, I have not been able to do this binding successfully with the ComboBox control.
With my Person class looking basically like this:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Gender { get; set; }
}
I am using the ComboBox's DataSource property to bind to a Dictionary and setting the ValueMember and DisplayMember accordingly. Doing so successfully populates that ComboBox with my reference data (display value and coded value) at run-time.
Dictionary<int, string> genderValues = repository.GetGenderValues()
cboGender.DataSource = new BindingSource(genderValues, null);
cboGender.ValueMember = "Key";
cboGender.DisplayMember = "Value";
However, when I try to bind my Person object's property 'Gender' to this ComboBox, following the pattern that worked with other control types (TextBox, CheckBox, etc.)
cboGender.DataBindings.Add(new Binding("ValueMember", _currentPerson, "Gender"));
The value of Gender on my person object is always NULL even after selecting an item in the Gender ComboBox. Perhaps I am overlooking an additional step needed for binding the ValueMember of a ComboBox to a object property?
ValueMember is a string and it does not change with a different selection in the combobox. You need to use one of the Selected... properties. Since it's an int, use SelectedValue.
Maybe this will help:
cboGender.DataBindings.Add("SelectedIndex", genderValues, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
I have a combobox that's set to DropDownStyle=DropDownList (meaning users can't type anything, just select from dropdown). The combo contains a list of states.
I am trying to bind the selected text value to _model.StateBar, but my code doesn't seem to update the property of the object.
I've tried both of the following:
cboStates.DataBindings.Add("Text", _model, "StateBar")
cboStates.DataBindings.Add("SelectedItem", _model, "StateBar")
cboStates.DataBindings.Add("SelectedValue", _model, "StateBar")
I just need to bind it one way: updates from the control need to end up on the object.
Binding to ComboBox.SelectedValue should work, but only in case when you add items through ComboBox.DataSource.
public class Model
{
public string StateBar { get; set; }
}
// In the form
var states = new List<string> { "Alabama", "California" };
combobox.DataSource = states;
combobox.DataBindings.Add("SelectedValue", _model, "StateBar", true, DataSourceUpdateMode.OnPropertyChanged);
Binding to SelectedItem should work in all cases.
I've a custom-made user control:
public class CustomUC : UserControl
{
[Bindable(true)]
private string PropertyX { get { ... } ; set { ... }; }
}
I'm using it from another form. On this form I'm using a DataSource.
The problem is that I'm unable to figure out how to bind this PropertyX to some field of my DataSource.
EDIT
My DataSource is a System.Windows.Forms.BindSource object I've created on design-time. I've set a class to this datasource in order to be linked to a ClassA. This class has a property Notes I'd like to bind to CustomMadeUCPropertyX...
You should try following way to bind property by coding:
object.DataBindings.Add("PropertyX", YourDataSrouce, "columnname in your datasource")
I have a class containing details of my customer.
class CustomerData : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get
{ return _Name }
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
// Lots of other properties configured.
}
I also have a list of CustomerData values List<CustomerData> MyData;
I currently databinding an individual CustomerData object to textboxes in the below way which works fine.
this.NameTxtBox.DataBindings.Add("Text", MyCustomer, "Name", false, DataSourceUpdateMode.OnPropertyChanged);
I'm struggling to find a way to bind each object in the list MyData to a ListBox.
I want to have each object in the MyData list in displayed in the ListBox showing the name.
I have tried setting the DataSource equal to the MyData list and setting the DisplayMember to "Name" however when i add items to the MyData list the listbox does not update.
Any ideas on how this is done?
I found that List<T> will not allow updating of a ListBox when the bound list is modified.
In order to get this to work you need to use BindingList<T>
BindingList<CustomerData> MyData = new BindingList<CustomerData>();
MyListBox.DataSource = MyData;
MyListBox.DisplayMember = "Name";
MyData.Add(new CustomerData(){ Name = "Jimmy" } ); //<-- This causes the ListBox to update with the new entry Jimmy.
I have a class named OrganizerNote with fields: public long id; public DateTime CreationDate; public string Title; public string Note;
Also I have a class public class XMLOrganizer that has 1 field:
public List<OrganizerNote> Notes=new List<OrganizerNote>();
For example, I have several objects:
OrganizerNote n1 = new OrganizerNote();
OrganizerNote n2 = new OrganizerNote();
with some data in fields.
Then I create 1 object
XMLOrganizer xmlOrg = new XMLOrganizer();
xmlOrg.Notes.Add(n1);
xmlOrg.Notes.Add(n2);
So I need to bind each xmlOrg.Notes[0] ... xmlOrg.Notes[i] to Row[i] in datagridView control. And fields values would be in columns.
How could I do so?
make
List<OrganizerNote> Notes=new List<OrganizerNote>();
into
BindingList<OrganizerNote> Notes=new BindingList<OrganizerNote>();
and then create a binding source on the form bind that to the database and then set the binding datasource to the Notes field on the XMLOranizer