简体   繁体   中英

Binding not changing selected value of Combobox

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM