Difference between SelectedItem, SelectedValue and SelectedValuePath

后端 未结 5 806
轻奢々
轻奢々 2020-11-22 01:20

What is the difference betweeen the following:

  • SelectedItem
  • SelectedValue
  • SelectedValuePath

All these dependency properties ar

5条回答
  •  醉话见心
    2020-11-22 01:28

    To answer a little more conceptually:

    SelectedValuePath defines which property (by its name) of the objects bound to the ListBox's ItemsSource will be used as the item's SelectedValue.

    For example, if your ListBox is bound to a collection of Person objects, each of which has Name, Age, and Gender properties, SelectedValuePath=Name will cause the value of the selected Person's Name property to be returned in SelectedValue.

    Note that if you override the ListBox's ControlTemplate (or apply a Style) that specifies what property should display, SelectedValuePath cannot be used.

    SelectedItem, meanwhile, returns the entire Person object currently selected.

    (Here's a further example from MSDN, using TreeView)

    Update: As @Joe pointed out, the DisplayMemberPath property is unrelated to the Selected* properties. Its proper description follows:

    Note that these values are distinct from DisplayMemberPath (which is defined on ItemsControl, not Selector), but that property has similar behavior to SelectedValuePath: in the absence of a style/template, it identifies which property of the object bound to item should be used as its string representation.

提交回复
热议问题