C# Binding data to combobox - c#

SOLVED: My frontcolor of the combobox was set to White, hence I didn't see them.
I know this has been asked alot, I've been searching on Google but haven't found a solution to my problem. Nothing seems to work.
I want to bind data to my combobox (which comes from a database) but it's always blank. But when I bind the same data to a list, data appears..
XAML
<ComboBox x:Name="cboCPType" Style="{StaticResource comboboxenright}" ItemsSource="{Binding CPTypeList}" DisplayMemberPath="Name" SelectedValue="SelectedPerson.JobRole.ID"/>
viewmodel
private ObservableCollection<ContactPersonType> _CPTypeList;
public ObservableCollection<ContactPersonType> CPTypeList
{
get
{
return _CPTypeList;
}
set
{
_CPTypeList = value;
OnPropertyChanged("CPTypeList");
}
}
model
private String _Name;
public String Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public static ObservableCollection<ContactPersonType> GetTypes()
{
ObservableCollection<ContactPersonType> lijst = new ObservableCollection<ContactPersonType>();
String sql = "SELECT * FROM ContactpersoonType";
DbDataReader reader = Database.GetData(sql);
while (reader.Read())
{
ContactPersonType cType = new ContactPersonType();
cType.Id = reader["ID"].ToString();
cType.Name = reader["Naam"].ToString();
lijst.Add(cType);
}
return lijst;
}

Well, you must have a class with a property named Name, but you don't show your code or what/where/how you actually initialize CPTypeList.
Have a look at this article I've wrote:
Understanding SelectedValue, SelectedValuePath, SelectedItem & DisplayMemberPath + Demo.
Hopefully it'll clear some things for you :)
Also, have a look at the end of this answer to make sure you have the datacontext set.

Related

Populate a WPF Combobox from Service

I am trying to populate my combobox from a service response. The service returns an array of object like following
MyService.FirmSocial[] firmSocialList = client.GetActiveSocialMediaTypes();
I have checked, the firmSocialList populates properly. I need to populate my combobox with these values.
I have tried this in my code behind
cbSocialMediaTypes.ItemsSource = firmSocialList;
cbSocialMediaTypes.DisplayMemberPath = "socialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "socialMediaType";
I also tried the same thing on the XAML side, but all I am getting is bunch of empty strings in my combobox. The thing is though, the number of elements matches with the item count of the combobox (of empty strings).
And yes, the property names of the FirmSocial object is correct.
The FirmSocial class
public class FirmSocial
{
private int socialMediaType;
private string socialMediaValue;
public int SocialMediaType
{
get
{
return socialMediaType;
}
set
{
socialMediaType = value;
}
}
public string SocialMediaValue
{
get
{
return socialMediaValue;
}
set
{
socialMediaValue = value;
}
}
}
And I have also tried this in my XAML section;
<ComboBox x:Name="cbSocialMediaTypes" HorizontalAlignment="Left" Margin="56,8,0,0" VerticalAlignment="Top" Width="211"
ItemsSource="{Binding firmSocialList}"
DisplayMemberPath="socialMediaType"
SelectedValuePath="socialMediaType" />
Thanks.
DisplayMemberPath is case sensitive.
DisplayMemberPath="socialMediaType"
is saying trying to bind to your private field, not your public property. Try:
DisplayMemberPath="SocialMediaType"
The problem is that you set the DisplayMemberPath and the SelectedValuePath to a private fields instead of a public properties.
cbSocialMediaTypes.DisplayMemberPath = "socialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "socialMediaType";
Change it to
cbSocialMediaTypes.DisplayMemberPath = "SocialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "SocialMediaType";

Binding ListBox.SelectedItem to Property

This might be a duplicate question, but I'm unable to find a good answer. All the answers like Binding WinForms ListBox to object properties don't work on my WinForm. I'll explain.
I have a list of Firms that I show in a ListBox. I would like when the SelectedItem changes, that it updates a property on my model. So that I can read the Firms properties.
// the classes
public class Firm
{
public string Name { get; set; }
public int Id { get; set; }
// more properties ...
}
public class MyModel : INotifyPropertyChanged
{
private Firm _firm = new Firm();
public Firm Firm
{
get { return _firm; }
set
{
if (Equals(value, _firm)) return;
_firm = value;
OnPropertyChanged();
}
}
// more properties and OnPropertyChanged() ...
}
// the form
private MyModel Model;
public void MyForm(List<Firm> firms)
{
lstFirm.DataBindings.Add("SelectedItem", Model, "Firm",
true, DataSourceUpdateMode.OnPropertyChanged);
lstFirm.DisplayMember = "Name";
lstFirm.ValueMember = "Id";
lstFirm.DataSource = firms;
}
public void lstFirm_SelectedIndexChanged(object sender, EventArgs e)
{
// Do something with Model.Firm
}
The problem is that Model.Firm null is. Does anybody have an idea what I need to do to make a databinding between the ListBox and the Model? I bind other stuff on my WinForm (such as TextBoxes to String properties) and those work nicely.
From what I can see, your code never sets Model.Firm... Where's the constructor for MyModel? If you don't provide one, Model.Firm will stay null unless you explicitly set it. Here's an example constructor:
public MyModel(Firm firm)
{
_firm = firm;
}
Also, Equals() doesn't do what you think it does. Instead of if (Equals(value, _firm)) return;, use this: if (value == _firm) return;
Ok, so after a weekend of testing, I figured it out.
I was debuging in the SelectedIndexChanged event and didn't see the change in my Model.Firm just yet. But as the SelectedItemChanged event is only internal, I couldn't use that and that's where the databinding on SelectedItem applies the values to databound items.
Now the reason why the change isn't visible yet, is because the SelectedItemChanged is only fired after the SelectedIndexChanged is executed. So internally in the ListBox control, it probably looks like
this.SelectedIndex = value;
this.SelectedItem = FindItem(value);
this.SelectedIndexChanged(/*values*/);
this.SelectedItemChanged(/*values*/); // Apply databinding changes
So it's quite normal that you don't see the changes, before the change has occured. And I didn't know this, so I was kinda stumped why the SelectedItem (who was displaying the changed value) wasn't copied over to the databound model property.
So I didn't have to change anything major to get it all working. :)

How to bind datatable value in combobox which has static value?

I have a ComboBox with few static values.
<ComboBox Name="cmbBoxField" Grid.Column="4" Grid.Row="2" Style="{StaticResource comboBoxStyleFixedWidth}" ItemsSource="{Binding}" ></ComboBox>
MVVMModle1.cmbBoxField.Items.Add(new CustomComboBoxItem("Text Box", "0"));
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Pick List", "1"));
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Check Box", "2"));
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Radio Button", "3"));
When I am saving the data in Database table it is getting saved.
((CustomComboBoxItem)(MVVMModle1.cmbBoxField.SelectedValue)).Value.ToString();
Now when I am trying to Edit my form and binding the value again to combobox it is not showing the value.
MVVMModle1.cmbBoxField.SelectedValue = dtDataList.Rows[0]["ControlList"].ToString().Trim();
Someone please help me in this. How to bind selected value to the combobox?
There are quite a few problems with your code here:
You are setting the ItemsControl.ItemsSource property to the default binding (bind to the current data context), which is incorrect unless the DataContext is any type that implements IEnumerable, which it probably isn't.
If this is correct because the DataContext is, for example, an ObservableCollection<T>, then you still have an issue because you are adding items statically to the ComboBox instead of whatever the ItemsSource is.
Also, the type of items you are adding are CustomComboBoxItem, which I'm going to assume inherits from ComboBoxItem. Either way, you can't say the SelectedValue is some string since the values in the ComboBox are not strings.
You should really not have a collection of CustomComboBoxItem's, but instead a custom class that is in itself it's own ViewModel.
Now that that's been said, here is a suggested solution to your problem:
<ComboBox ItemsSource="{Binding Path=MyCollection}"
SelectedValue="{Binding Path=MySelectedString}"
SelectedValuePath="StringProp" />
public class CustomComboBoxItem : ComboBoxItem
{
// Not sure what the property name is...
public string StringProp { get; set; }
...
}
// I'm assuming you don't have a separate ViewModel class and you're using
// the actual window/page as your ViewModel (which you shouldn't do...)
public class MyWPFWindow : Window, INotifyPropertyChanged
{
public MyWPFWindow()
{
MyCollection = new ObservableCollection<CustomComboBoxItem>();
// Add values somewhere in code, doesn't have to be here...
MyCollection.Add(new CustomComboBoxItem("Text Box", "0"));
etc ...
InitializeComponent();
}
public ObservableCollection<CustomComboBoxItem> MyCollection
{
get;
private set;
}
private string _mySelectedString;
public string MySelectedString
{
get { return _mySelectedString; }
set
{
if (String.Equals(value, _mySelectedString)) return;
_mySelectedString = value;
RaisePropertyChanged("MySelectedString");
}
}
public void GetStringFromDb()
{
// ...
MySelectedString = dtDataList.Rows[0]["ControlList"].ToString().Trim();
}
}
You could alternatively not implement INotifyPropertyChanged and use a DependencyProperty for your MySelectedString property, but using INPC is the preferred way. Anyways, that should give you enough information to know which direction to head in...
TL;DR;
Take advantage of binding to an ObservableCollection<T> (create a property for this).
Add your items (CustomComboBoxItems) to the ObservableCollection<T>.
Bind the ItemsSource to the new collection property you created.
Bind the SelectedValue to some string property you create (take advantage of INPC).
Set the SelectedValuePath to the path of the string property name of your CustomComboBoxItem.
Can you use cmbBoxField.DataBoundItem()? If not target the source from the selected value, i.e. Get the ID then query the source again to get the data.
(CustomComboBoxItem)MVVMModle1.cmbBoxField.DataBoundItem();
When you bind a datasource it is simpler to do it like this:
private List GetItems(){
List<CustomComboBoxItem> items = new List<CustomComboBoxItem>();
items.Add(new CustomComboBoxItem() {Prop1 = "Text Box", Prop2 = "0"});
//...and so on
return items;
}
Then in your main code:
List<CustomComboBoxItem> items = this.GetItems();
MVVMModle1.cmbBoxField.DisplayMember = Prop1;
MVVMModle1.cmbBoxField.ValueMember = Prop2;
MVVMModle1.cmbBoxField.DataSource = items;
This will then allow your selected value to work, either select by index, value or text
var selected = dtDataList.Rows[0]["ControlList"].ToString().Trim();
MVVMModle1.cmbBoxField.SelectedValue = selected;

WPF Data Binding TooWay XAML to C# and C# to XAML

I have XAML code:
<TextBox Name="textBoxMask1"/>
<TextBox Name="textBoxMask2"/>
<TextBox Name="textBoxMask3"/>
...
<TextBox Name="textBoxMask9"/>
and class in C#:
private static string mask1;
public static string Mask1
{
get { return mask1; }
set { mask1 = value; }
}
private static string mask2;
public static string Mask2
{
get { return mask2; }
set { mask2 = value; }
}
private static string mask3;
public static string Mask3
{
get { return mask3; }
set { mask3 = value; }
}
....
private static string mask9;
public static string Mask9
{
get { return mask9; }
set { mask9 = value; }
}
And I want to bind these TextBoxes with Properties -> textBoxMask1 with Mask1 etc.
Earlier I did this by TextChanged, but I want to make Binding. TooWay Binding, because I want to predefine Mask1, Mask2, Mask3, ..., Mask9 in another C# class, and maybe later change these values - also in some C# code - and I want my changes, to be visible in layout (XAML) and in C# code - so ex. changing Property Mask1 from C# will change Text in TextBox textBoxMask1, and changing Text in textBoxMask1 will change Property Mask1.
I don't understand, how to make connection (binding) between objects XAML and C#.
For a normal Binding you don't need your properties to be static, just public. Here an example:
C# code (for one property)
private string mask1;
public string Mask1
{
get { return mask1; }
set
{
mask2 = value;
RaisePropertyChanged("Mask1");
}
}
It's really important for the binding that the class containing the properties implements the INotifyPropertyChanged interface, and that you raise the corresponding event in the setter of each property. Another option is to make all properties DependecyProperty, but it is usually overkill.
As for the XAML:
<TextBox Name="textBoxMask1" Text="{Binding Mask1, Mode=TwoWay}"/>
(TwoWay Binding is the default for the Text property, but it does not hurt to put it explicitly).
Just make sure that the DataContext of the object containing your TexBoxes (usually an UserControl) is set to a valid instance of your C# class.
By the way, this is a very basic question, that's why you got a negative vote and no answers before mine. What is expected is that you ask a question that poses a real problem for you, with a very specific answer, not something like "teach me how to do this".
If this answers your question don't forget to mark it as answer (the "tick" mark on the top left). A vote up would be also appreciated.
Hope it helps, regards.

WPF binding to a datagrid's SelectedItem

I have a datagrid bound to an ObservableCollection<MyClass>, and I have another datagrid which has two DataGridTextColumns - Name and Value. The Name column is prepopulated with the names of properties whose values should be displayed in the Value column. MyClass implements INotifyPropertyChanged, so any change in the properties of MyClass objects updates the first datagrid. Now, I would like to display the properties of the currently selected object (SelectedItem) of the first datagrid in the Value column of the second datagrid and see the property changes as they happen, like in the first datagrid. How can I accomplish this?
If you wonder about the reason, only some of the properties are displayed in the original datagrid, so the other one should display almost all of them. Is datagrid even a good choice for displaying properties in 2 columns or should I consider some other control?
This sounds like one convenient solution to a fairly common problem.
The easiest way to do this with two data grids will be for you to use some code behind and reflection. First define a type to display the Name and value of each property:
class PropertyModel {
private readonly string _name = "";
private readonly object _value = null;
public PropertyModel(string name, object value) {
_name = name ?? "";
_value = _value;
}
public string Name {
get { return _name; }
}
public object Value {
get { return _value; }
}
}
Then add an event handler to your code-behind to handle selection changes on your first datagrid:
private void _onDataGrid1SelectionChanged(object sender, SelectedCellsChangedEventArgs e) {
if (e.AddedCells.Count > 0) {
var props = new Collection<PropertyModel>();
var obj = _dataGrid1.SelectedItem;
foreach(var prop in obj.GetType().GetProperties()) {
props.Add(new PropertyModel(prop.Name, prop.GetValue(obj, null)));
}
_dataGrid2.ItemsSource = props;
}
}
Note that the code above is very rough, and will only work if DataGrid1 has SelectionMode set to Single. However this is a good place to start, if you are willing to do it quick and dirty (with an event handler).
Another great solution is to use row details.
This is a pretty good intro tutorial on using row details.
Of course you should also read the msdn article on the subject.

Categories