這是一篇懶文:)
          只因在大名鼎鼎的stackoverflow上遇到了牛逼哄哄的 Chris Anderson
          下面我們來看看牛人是怎樣火眼金睛識(shí)破這三個(gè)神秘家伙的:
          以下內(nèi)容完全轉(zhuǎn)自stackoverflow

          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 aProduct 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 SelectedValueproperty 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... :)

          Chris

















          posted on 2015-06-11 10:55 Ying-er 閱讀(2809) 評(píng)論(0)  編輯  收藏 所屬分類: WPF
          主站蜘蛛池模板: 资源县| 陕西省| 赤峰市| 宜川县| 新化县| 隆回县| 襄汾县| 苏尼特右旗| 定结县| 阿巴嘎旗| 崇左市| 昆山市| 应城市| 北碚区| 娄烦县| 多伦县| 连城县| 北宁市| 平顺县| 甘孜| 逊克县| 鲜城| 基隆市| 贵阳市| 民县| 柳河县| 阿拉尔市| 康定县| 兰西县| 灵寿县| 西昌市| 盐山县| 京山县| 若羌县| 九江县| 黔西县| 保定市| 大余县| 丹寨县| 米林县| 手机|