簡體   English   中英

綁定不更改組合框的選定值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM