I thought this would be easy, but now I don´t know how to do it exactly.
In a WPF-application, i go from one window to another by clicking a contextmenu-item. My constructor for the new window looks like this:
public Bearbeitung(int loginid, String art)
On the Window, there´s a checkbox filled with a list. What I want is, that the default selected item in my checkbox is art.
Ok, I checked if the String is in the list, but now I don´t know how to set it to the selecteditem in the combobox.
How can I manage this?
EDIT: I already tried
combobox.SelectedItem = art;
...that doesn´t work!
EDIT2:
Here´s the code:
List<String> feld = new List<string>();
feld = agrep.GetFelder(loginid);
foreach (String s in feld)
{
cbFeld.Items.Add(s);
}
if (cbFeld.Items.Contains(art))
{
MessageBox.Show("It contains it");
cbFeld.SelectedItem = art;
}
The messagebox isn´t shown!
If the list items are just strings, you can simply do
myComboBox.SelectedItem = art;
If your ComboBox only contains strings you should be able to just set the SelectedItem
cb.SelectedItem = art;
If it does not only contain strings you might want to change that, e.g.
cb.ItemsSource = new string[] { "Item 1", "Item 2" };
If you have complex objects you will want to set the SelectedValue and SelectedValuePath instead.
try
cbFeld.Text = art;
this should work.
Ok, I just solve it. The problem was, that when I give the string to the other window, a blank has been added. Thanks to you all!
Related
Sorry if it has some obvious solution, but I am trying to solve it for hours but could not find a solution.
I use several ComboBoxes in my WindowsFormsApplication to relate ids with names. The problem is that when a user select an item from the combobox list, it works fine, but when he types an item, the SelectedValue property of the combobox is null.
To simulate the problem, I created a from with one button and a combobox.
In my actual application, I populate the comboboxes with data from tables in a sqlserver database, but for simplicity, here I populate it with a list:
public Form1()
{
InitializeComponent();
List<KeyValuePair<short,short>> l = new List<KeyValuePair<short,short>>();
l.Add(new KeyValuePair<short,short>(1,10));
l.Add(new KeyValuePair<short,short>(2,20));
l.Add(new KeyValuePair<short,short>(3,30));
this.comboBox1.DataSource = l;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedValue == null)
MessageBox.Show("NULL");
else
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}
For example, when user select the second item (20) from the list and clicks on the button, messagebox shows 2 as it is expected, but if he types the number 20 into the combobox, the SelectedValue is null.
This problem could be solved by changing the style of combobox:
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
But it prevents user to type into the combobox, so I am forced to use the default ComboBoxStyle.DropDown.
Thats because the combo box does not select the item that you have typed. Add this option
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
Then it will select the item when ever it was able to find it.
By default it is set to AutoCompleteMode.None.
(I think) This is mainly designed for suggestions but it can solve your problem here. also if you want to show the suggestions:
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
by default it is set to AutoCompleteSource.None.
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode%28v=vs.110%29.aspx
An option that you have is using the EventHandler on the comboBox1.TextChange event that will allow you to personally handle how the text is translated into the different options.
This can be added to the designer (similar to a button).
this.comboBox1.TextChanged += new System.EventHandler(this.UpdateValue);
Then depending on how want to read the results and see if you have a match you can create a converter, use another key/value, or you can do a boring old tryparse as I will show you. You will need the list as a property that you can reference to see if you have found the proper results.
private void UpdateValue(object sender, EventArgs e)
{
short result;
if (short.TryParse(comboBox1.Text, out result))
{
var matches = from val in l where val.Value == result select val.Key;
{
foreach (short m in matches)
this.comboBox1.SelectedValue = m;
}
}
}
Good luck! Let me know if you need more examples with the event handler. Don't forget to vote.
I'm having an issue when I select an item from a DataGridComboBoxColumn. The cell wont display the name of the item I've chosen after I move focus to the next cell. This is the code I have:
DataGridComboBoxColumn cb1 = new DataGridComboBoxColumn();
cb1.ItemsSource = listOStrings;
cb1.TextBinding = new Binding("listOfStrings");
e.Column = cb1;
e.Column.Header = "SomeTitle";
Where listOfStrings is a list that is being updated by the user. I have another DataGridComboBoxColumn that has its ItemSource set to a list of strings that isn't being updated. That one displays the text just fine, although the code for the two is the same. I'm wondering why my cb1 combo box wont display the values after leaving the cell but the other one does?
When a binding in WPF is hooked up to a non-static source, the underlying source needs to implement iNotifyPropertyChanged. In your case you may want to use an ObservableCollection as answered here: Why does a string INotifyPropertyChanged property update but not a List<string>?
In your case, it would look something like:
private ObservableCollection<string> _listOStrings = new ObservableCollection<string>();
public ObservableCollection<string> ListOStrings
{
get
{
return _listOStrings;
}
set
{
_listOStrings = value;
OnPropertyChanged("ListOStrings");
}
}
For more information on iNotifyPropertyChanged from MSDN, see:
See: https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx
I've never done binding the way you are doing it - have you considered moving the UI to the XAML and databinding to a ViewModel? Here is an awesome step by step example on databinding comboboxes. You would just have the combobox be a column within the DataGrid also - similar to this.
I am having a problem to change a selected item in a drop-down.
The way I use is (a property in the code behind which sets the new selection):
public char Candy
{
set
{
var newSelection = ddlCandy.Items.FindByValue(value.ToString());
ddlCandy.ClearSelection();
newSelection.Selected = true;
}
}
Is this a recommended and proper way?
recommended approach is to simply assign the SelectedValue property with the Value you have and the DropDownList control will find and select the proper item for you, if any.
Safe way is fist Find the given item from DropDownList and set it as SelectedValue
ListItem oListItem = DropDownList1.Items.FindByValue("yourValue");
if(oListItem != null)
{
DropDownList1.SelectedValue = oListItem.Value;
}
if you directly assign SelectedValue it may through an exception if it is not exist in the list like bellow.
'DropDownList' has a SelectedValue which is invalid because it does
not exist in the list of items.
I usually prefer to use SelectedValue:
DropDownList1.SelectedValue = "Foo";
I'm at a bit of a loss on this one and haven't been able to find anything helpful in my searches so I'm hopeful someone can help me out here.
I've got a RadioButtonList that I'm adding a List of dynamically created ListItems, where I set both the text and the value for each item. On DataBind for the RadioButtonList the Value for the ListItem gets replaced by the Text, which just doesn't seem to make sense to me.
I can see on the client side when I look in Firebug that the label and the value on the input are the same, and the value is nowhere to be seen.
Has anyone else had any experiences like this, or does anyone know where I might be going wrong?
var rbList = new List<ListItem>();
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
rbList.Add(li);
}
radioButtonList.DataSource = rbList;
radioButtonList.DataBind();
Should you be using Databinding here? Can you not just add your ListItems to the radio button list directly?
I would imagine that the Databinding is getting confused about how to bind your list so is just using ToString on each of your elements which seems to just return the Text Property. This is then being used as both the Text and the Value.
You probably just want to create your items and add them straight to your Radio button control as follows:
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
radioButtonList.Items.Add(li);
}
For those, whose are still struggling - do not forget to fill DataValueField, if control has it. Simply provide string name of your value property and you will be fine.
As Chris mentioned - Databinding was in fact confused and therefore DataValueField exists. My problem was with basic asp:DropDownList.
enter code hereI have a TabControl on a Form and in the TabPages there are ComboBoxes.
When the form OnLoad, I populate the ListItems in the ComboBoxes and the attempt to set default values to string.Empty.
However, the ComboBox.SelectedText = string.Empty only works for the first TabPage. The other ComboBoxes ignore the command and take the default value as the first item in the list. Why is this so? How can I overcome it?
The ComboBoxes are all set up by this function
public static void PrepareComboBox(ComboBox combobox, FieldValueList list)
{
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
combobox.AutoCompleteMode = AutoCompleteMode.Suggest;
combobox.DataSource = list.DataSource;
combobox.DisplayMember = list.DisplayMember;
combobox.ValueMember = list.ValueMember;
combobox.Text = string.Empty;
combobox.SelectedText = string.Empty;
}
I found that the reason could be that the ComboBox is not "active" until they are at least shown once. You can see that when the TabPage is selected for the first time, it takes a slightly longer time to load. I suppose it is creating/initialising the child controls for the first time.
For that, I call tabControl.SelectTab() before modifying the value properties and it worked... although it feels like a hack.
This is due to databinding. Not much you can do about it, except prefix the datasource with an empty/dummy entry.