zoukankan      html  css  js  c++  java
  • difference-between-selecteditem-selectedvalue-and-selectedvaluepath(转)

    370down voteaccepted

    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 IDproperty).

    • 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 CategoryIDproperty on the Product object. This is where the SelectedValuePath and the SelectedValueproperties 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" />
    

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

    http://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath

  • 相关阅读:
    Array对象
    属性描述对象
    Object对象
    console对象与控制台
    编程风格
    错误处理机制
    13、百钱买百鸡之数学优化
    12、c程序中编程,统计一行中数字字符的个数。
    10、输入某年某月某日,判断这一天是这一年的第几天?
    9、c语言输入一个整数怎么分别输出它的每位上的数字
  • 原文地址:https://www.cnblogs.com/maoyan/p/6553776.html
Copyright © 2011-2022 走看看