一种解决方案是将组合框的ItemsSource设置为列表(例如:“DeviceNameList”),并将此组合框的SelectedItem设置为与列表中元素类型匹配的变量(SelectedDeviceName)。
现在,当您加载编辑屏幕时,它会将列表绑定到组合框并显示您设置的变量。
您必须编写一些代码来检查所选项目是否出现在列表中,如果没有,您可以将值设置为零。
例子:
XAML 代码:
<ComboBox ItemsSource="{Binding Path=DeviceNameList}" SelectedItem="{Binding Path=SelectedDeviceName}" />
设置 selectedItem 的代码:
/// <summary>
/// Gets or sets SelectedDeviceName.
/// </summary>
public ObservableCollection<string> DeviceNameList
{
get
{
return mDeviceNameList;
}
set
{
mDeviceNameList = value;
}
}
/// <summary>
/// Gets or sets SelectedDeviceName.
/// </summary>
public string SelectedDeviceName
{
get
{
return mSelectedDeviceName;
}
set
{
mSelectedDeviceName = value;
NotifyPropertyChanged("SelectedDeviceName");
}
}
/// <summary>
/// Event PropertyChanged
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Function NotifyPropertyChanged
/// </summary>
/// <param name="property">
/// The property.
/// </param>
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}