2012-11-23 30 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