如何使用绑定获取combobox selectedItem值
我的组合框:如何使用绑定获取combobox selectedItem值,combobox,silverlight-5.0,xaml-binding,Combobox,Silverlight 5.0,Xaml Binding,我的组合框: <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
<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>
已将两个静态项作为地块和属性添加到combobox,并希望获取这些值
使用绑定
我已为SelectedItem提供绑定,并且我的绑定字段适用于
使用上面的代码,我得到的值在适用的_中为null
编辑:我为我以前忘记的所选项目添加了Mode=Two-Way
但它并没有像“PropMgmt.Controls.pmComboBoxItem”那样获得名称空间的值
请帮助..您可以为组合框创建集合,而不是向组合框中添加静态项。例如,创建类,如:
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; }
}
}
然后在视图模型中添加以下代码:
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);
ObservableCollection适用于_KeyValues=新的ObservableCollection();
KeyValuePair k1=新的KeyValuePair(){Key=“1”,Value=“Parcel”};
KeyValuePair k2=新的KeyValuePair(){Key=“2”,Value=“Property”};
适用于密钥值的密钥。添加(k1);
适用于密钥值的密钥。添加(k2);
然后在xaml中添加以下内容:
<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>
希望这能解决你的问题