[英]Binding not changing selected value of Combobox
我有ComboBox
<ComboBox Height="23" Name="DriveSelection" Width="120"
ItemsSource="{Binding Path=FixedDrives}"
DisplayMemberPath="Name"
SelectedItem="{Binding DriveSelection_SelectionChanged }"
IsSynchronizedWithCurrentItem="True"
IsEnabled="{Binding DriveIsEnabled}"
/>
在我的viewModel
它看起来像这样:
public PathSelectionPageViewModel(PathSelectionPage _page)
{
this.page = _page;
this.root = Path.GetPathRoot(App.Instance.PathManager.InstallRoot).ToUpperInvariant();
this.DriveSelection = this.root;
this.DriveSelection_SelectionChanged = new DriveInfo(this.root);
this.DriveIsEnabled = App.Instance.PathManager.CanModify;
this.RunText = App.Instance.InstallationProperties.InstallationScript.Installer.Name;
}
public ObservableCollection<DriveInfo> FixedDrives
{
get
{
if (this.fixedDrives != null)
return this.fixedDrives;
this.fixedDrives = new ObservableCollection<DriveInfo>(Enumerable.Where<DriveInfo>((IEnumerable<DriveInfo>)DriveInfo.GetDrives(), (Func<DriveInfo, bool>)(driveInfo => driveInfo.DriveType == DriveType.Fixed)));
return this.fixedDrives;
}
}
public DriveInfo DriveSelection_SelectionChanged
{
get
{
return this.driveSelection;
}
set
{
if (value == this.driveSelection)
return;
this.driveSelection = value;
UpdatePathManager();
this.OnPropertyChanged("DriveSelection_SelectionChanged");
}
}
如您所见,我将hardrives列表作为itemSource
绑定到组合框。 然后,如果需要,我将在此行中更改所选项目:
this.DriveSelection_SelectionChanged = new DriveInfo(this.root);
例如。 this.root指向驱动器E
因此组合框选择应更改为E,但现在它仍挂在C
。 我的绑定错误或其他地方有错误?
您在这一行用new创建的对象
this.DriveSelection_SelectionChanged = new DriveInfo(this.root);
数据绑定到ItemsSource的FixedDrivers列表中未包含。 如果您选择了FixedDrivers中的一项,则将选择正确的项
var selectedItem = this.FixedDrives.FirstOrDefault(d => d.Name == this.Root);
this.DriveSelection_SelectionChanged = selectedItem;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.