2012-11-23 29 views
1

我的组合框组合框获得价值则selectedItem:如何使用绑定

<pmControls:pmComboBox Grid.Row="3" Grid.Column="1" Margin="3" 
    SelectedItem="{Binding Applicable_For,Mode=Two Way}" DisplayMemberPath="Applicable_For" 
    SelectedValuePath="Applicable_For"> 

    <pmControls:pmComboBoxItem Content="Parcel" ></pmControls:pmComboBoxItem> 
    <pmControls:pmComboBoxItem Content="Property"></pmControls:pmComboBoxItem> 

    </pmControls:pmComboBox> 

增加了2个静态项目组合框的包裹和财产,想用结合对这些值 。

我给了SelectedItem绑定,我的绑定字段是App​​licable_For。

使用上面的代码将适用_For中的值设置为null。

编辑:我已经添加Mode=Two Way选定的项目,我忘了以前。

但不是获得价值像命名空间“PropMgmt.Controls.pmComboBoxItem”

请帮助..

回答

2

不是添加静态项组合框,你可以为它创建的集合。为前。以下

 ObservableCollection<KeyValuePair> applicable_For_KeyValues = new ObservableCollection<KeyValuePair>(); 

     KeyValuePair k1 = new KeyValuePair() { Key = "1", Value = "Parcel" }; 
     KeyValuePair k2 = new KeyValuePair() { Key = "2", Value = "Property" }; 

     applicable_For_KeyValues.Add(k1); 
     applicable_For_KeyValues.Add(k2); 
XAML中添加

然后:创建类,如:

public class KeyValuePair 
{ 
    string key; 

    public string Key 
    { 
     get { return key; } 
     set { key = value; } 
    } 
    string value; 

    public string Value 
    { 
     get { return this.value; } 
     set { this.value = value; } 
    }  

} 

然后在您的视图模型中添加以下代码

<pmControls:pmComboBox Grid.Row="3" Grid.Column="1" Margin="3" 
ItemsSource="{Binding Applicable_For_KeyValues}" 
SelectedValue="{Binding Applicable_For,Mode=TwoWay}" SelectedValuePath="Value"> 
       <pmControls:pmComboBox.ItemTemplate > 
        <DataTemplate> 
         <TextBlock Text="{Binding Value}"></TextBlock> 
        </DataTemplate> 
       </pmControls:pmComboBox.ItemTemplate>   

      </pmControls:pmComboBox> 

希望这会解决您的问题。

+0

It Works thanks – Gayatri