Combobox Selecteditem VS Selectedvalue

ComboBox SelectedItem vs SelectedValue

The ComboBox control inherits from the ListControl control.

The SelectedItem property is a proper member of the ComboBox control. The event that is fired on change is ComboBox.SelectionChangeCommitted

ComboBox.SelectionChangeCommitted

Occurs when the selected item has changed and that change is displayed in the ComboBox.

The SelectedValue property is inherited from the ListControl control.
As such, this property will fire the ListControl.SelectedValueChanged event.

ListControl.SelectedValueChanged

Occurs when the SelectedValue property changes.

That said, they won't fire the INotifyPropertyChanged.PropertyChanged event the same, but they will anyway. The only difference is in the firing event. SelectedValueChanged is fired as soon as a new selection is made from the list part of the ComboBox, and SelectedItemChanged is fired when the item is displayed in the TextBox portion of the ComboBox.

In short, they both represent something in the list part of the ComboBox. So, when binding either property, the result is the same, since the PropertyChanged event is fired in either case. And since they both represent an element from the list, the they are probably treated the same.

Does this help?

EDIT #1

Assuming that the list part of the ComboBox represents a property (as I can't confirm since I didn't write the control), binding either of SelectedItem or SelectedValue affects the same collection inside the control. Then, when this property is changed, the same occurs in the end. The INotifyPropertryPropertyChanged.PropertyChanged event is fired on the same property.

Difference between SelectedItem, SelectedValue and SelectedValuePath

Their names can be a bit confusing :). Here's a summary:

  • The SelectedItem property returns the entire object that your list is bound to. So say you've bound a list to a collection of Category objects (with each Category object having Name and ID properties). eg. ObservableCollection<Category>. The SelectedItem property will return you the currently selected Category object. For binding purposes however, this is not always what you want, as this only enables you to bind an entire Category object to the property that the list is bound to, not the value of a single property on that Category object (such as its ID property).

  • Therefore we have the SelectedValuePath property and the SelectedValue property as an alternative means of binding (you use them in conjunction with one another). Let's say you have a Product object, that your view is bound to (with properties for things like ProductName, Weight, etc). Let's also say you have a CategoryID property on that Product object, and you want the user to be able to select a category for the product from a list of categories. You need the ID property of the Category object to be assigned to the CategoryID property on the Product object. This is where the SelectedValuePath and the SelectedValue properties come in. You specify that the ID property on the Category object should be assigned to the property on the Product object that the list is bound to using SelectedValuePath='ID', and then bind the SelectedValue property to the property on the DataContext (ie. the Product).

The example below demonstrates this. We have a ComboBox bound to a list of Categories (via ItemsSource). We're binding the CategoryID property on the Product as the selected value (using the SelectedValue property). We're relating this to the Category's ID property via the SelectedValuePath property. And we're saying only display the Name property in the ComboBox, with the DisplayMemberPath property).

<ComboBox ItemsSource="{Binding Categories}" 
SelectedValue="{Binding CategoryID, Mode=TwoWay}"
SelectedValuePath="ID"
DisplayMemberPath="Name" />
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
}

public class Product
{
public int CategoryID { get; set; }
}

It's a little confusing initially, but hopefully this makes it a bit clearer... :)

Chris

Combobox.Text Vs combobox.Selecteditem Vs combobox.selectValue?

Here is my example.

private void comboSelectChanged(object sender, SelectionChangedEventArgs e)
{
textBox1.Text = comboBox1.SelectedItem.ToString();
textBox2.Text = comboBox1.Text;
textBox3.Text = comboBox1.SelectedIndex.ToString();
}

Items collection:
Sample Image

And the results:

One selected

Two selected

Three selected


SelectedItem: Gets or sets currently selected item in the ComboBox.

Based on ComboBox.SelectionChangeCommitted

Text: Gets or sets the text associated with this control. (Overrides Control.Text.)

setting the text value will change the current value of the combobox

SelectedValue: Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)

Based on ListControl.SelectedValueChanged

This question might be a duplicate of ComboBox SelectedItem vs SelectedValue.

Source msdn

Further reading at dotnetperls.

WPF C# Combobox blank SelectedItem SelectedValue

The SelectedValue/SelectedValuePath and SelectedItem properties are not meant to be used both at the same time.

SelectedValue and SelectedValuePath would be used when you are binding to a collection of objects but you want to return a specific property of the objects you are binding to instead the entire object.

I would suggest removing the SelectedValue and SelectedValuePath Bindings and simply rely on the SelectedItem Binding. You won't need the InteractionTrigger for the SelectionChanged event.

<ComboBox ItemsSource="{Binding Path=PracticesList}"
DisplayMemberPath="TaxName"
SelectedItem="{Binding Path=SelectedPractice}" />

Your VM would have something like this:

private Practice _mySelectedPractice;

public Practice SelectedPractice
{
get => _selectedPractice;
set => _selectedPractice = value;
}

When the ComboBox selection has changed your SelectedItem will be updated in your VM's SelectedPractice property.

In your VM's SelectedPractice setter, you can chose to do whatever else you need to when the selection has changed.

Here is another article that explains SelectedItem vs. SelectedValue and SelectedValuePath: Difference between SelectedItem, SelectedValue and SelectedValuePath

MVVM ComboBox SelectedIndex vs. SelectedItem

The first thing you tried was actually correct however there are a couple of things that let you down.

For example:

get { return context.GetShiftList(); }

Generally, this is not a good idea. Anyone calling this property will invoke the database call every time which will hurt performance and simply isn't needed.

A better idea would be to instead call a method to load the ShiftList property, such as this:

public void ReloadShiftList()
{
//TODO: Your logic here

ShiftList = ...
}

Note: Be sure to implement INotifyPropertyChanged on your ShiftList property in order to update the view whenever this property is changed.

As for your ComboBox, binding to the SelectedItem is certainly the preferred method. The reason why the selected item was blank on first load was because SelectedShift was set to null. To remedy this, you simply need to set the SelectedShift after you have loaded the ShiftList.

ShiftList = ...
SelectedShift = ShiftList.FirstOrDefault();

By setting the SelectedShift to the .FirstOrDefault() of the ShiftList, you will guarantee that there will always be something selected in the ComboBox (unless there are no items in the ShiftList).

Finally, your binding would look like this:

<ComboBox ItemsSource="{Binding ShiftList}"
SelectedItem="{Binding SelectedShift}"
... />

Should I use a Winforms combobox's SelectedItem, SelectedText, or SelectedValue?

SelectedValue is probably the best one to use

SelectedText will give you the selected text of the editable portion, Selected Item will return you the object and selected index will return you the index. Usually for applications SelectedValue is extracted and used.
Check out Combobox from MSDN

SelectedIndex   Gets or sets the index specifying the currently selected item.                (Overrides ListControl.SelectedIndex.)
SelectedItem Gets or sets currently selected item in the ComboBox.
SelectedText Gets or sets the text that is selected in the editable portion of a ComboBox.
SelectedValue Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)

ComboBox SelectedValue or SelectedItem Binding WPF C#

You probably want to use SelectedItem. That'll give the actual item that was bound to it. SelectedValue is determined by the SelectedValuePath property... which is just unnecessary in this case.
Also, you probably don't want to set the UpdateSourceTrigger to be Explicit. The default should be fine in that regard.

WPF MVVM ComboBox SelectedItem or SelectedValue not working

Have you tried implementing INotifyPropertyChanged in your viewmodel, and then raise the PropertyChanged event when the SelectedItem gets set?

If this in itself doesn't fix it, then you will be able to manually raise the PropertyChanged event yourself when navigating back to the page, and that should be enough to get WPF to redraw itself and show the correct selected item.

SelectedValue vs SelectedItem.Value of DropDownList

SelectedValue returns the same value as SelectedItem.Value.

SelectedItem.Value and SelectedItem.Text might have different values and the performance is not a factor here, only the meanings of these properties matters.

<asp:DropDownList runat="server" ID="ddlUserTypes">
<asp:ListItem Text="Admins" Value="1" Selected="true" />
<asp:ListItem Text="Users" Value="2"/>
</asp:DropDownList>

Here, ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue and both would return the value "1".

ddlUserTypes.SelectedItem.Text would return "Admins", which is different from ddlUserTypes.SelectedValue

edit

under the hood, SelectedValue looks like this

public virtual string SelectedValue
{
get
{
int selectedIndex = this.SelectedIndex;
if (selectedIndex >= 0)
{
return this.Items[selectedIndex].Value;
}
return string.Empty;
}
}

and SelectedItem looks like this:

public virtual ListItem SelectedItem
{
get
{
int selectedIndex = this.SelectedIndex;
if (selectedIndex >= 0)
{
return this.Items[selectedIndex];
}
return null;
}
}

One major difference between these two properties is that the SelectedValue has a setter also, since SelectedItem doesn't. The getter of SelectedValue is faster when writing code, and the problem of execution performance has no real reason to be discussed. Also a big advantage of SelectedValue is when using Binding expressions.

edit data binding scenario (you can't use SelectedItem.Value)

<asp:Repeater runat="server">
<ItemTemplate>
<asp:DropDownList ID="ddlCategories" runat="server"
SelectedValue='<%# Eval("CategoryId")%>'>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>


Related Topics



Leave a reply



Submit