Reputation: 4496
I have ComboBox
<ComboBox Height="23" Name="DriveSelection" Width="120"
ItemsSource="{Binding Path=FixedDrives}"
DisplayMemberPath="Name"
SelectedItem="{Binding DriveSelection_SelectionChanged }"
IsSynchronizedWithCurrentItem="True"
IsEnabled="{Binding DriveIsEnabled}"
/>
In my viewModel
it looks like this:
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");
}
}
As You can see I'm binding list of hardrives to the combobox as itemSource
. And then If needed I'm changing the selected item in this line:
this.DriveSelection_SelectionChanged = new DriveInfo(this.root);
Eg. this.root is pointing at drive E
so combobox selection should change to E but now its still hanging at C
.
My bindings are wrong or error is elsewhere?
Upvotes: 0
Views: 298
Reputation: 1222
The object you create with new at this line
this.DriveSelection_SelectionChanged = new DriveInfo(this.root);
isn't contained in the FixedDrivers list you databind to ItemsSource. If you select one of the items in the FixedDrivers the right item will be selected
var selectedItem = this.FixedDrives.FirstOrDefault(d => d.Name == this.Root);
this.DriveSelection_SelectionChanged = selectedItem;
Upvotes: 1