简体   繁体   中英

Combo box binding in mvvm

I am not able to find out Why is combo box binding not working?

I have a view model which looks like (2 properties)

    public ProcessMaintenanceDataObject CurrentProcess
    {
        get
        {
            return _CurrentProcess;
        }
        set
        {
            _CurrentProcess = value;
            OnPropertyChanged("CurrentProcess");
        }
    }

    public ObservableCollection<ProcessMaintenanceDataObject > Processes 
    {
        get
        {
            return _Processes;
        }
        set
        {
            _Processes = value;
            OnPropertyChanged("Processes");
        }
    }

    public ObservableCollection<FolderInfo> Folders
    {
        get
        {
            return _folders;
        }
        set
        {
            _folders = value;
            OnPropertyChanged("Folders");
        }
    }

The following is the ProcessMaintenanceDataObject definition

    [DataMember]
    public string ProcessName
    {
        get
        {
            return _ProcessName;
        }
        set
        {
            _ProcessName = value;
            OnPropertyChanged("ProcessName");
        }

    }


    [DataMember]
    public string Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
            OnPropertyChanged("Id");
        }
    }

    [DataMember]
    public string FolderId
    {
        get
        {
            return _FolderId;
        }
        set
        {
            _FolderId = value;
            OnPropertyChanged("FolderId");
        }
    }

    [DataMember]
    public FolderInfo Folder
    {
        get
        {
            return _Folder;
        }
        set
        {
            _Folder = value;
            if (_Folder != null)
                FolderId = _Folder.FolderId;
            OnPropertyChanged("Folder");
        }
    }

The FolderInfo class has FolderName and FolderId Property.

I have a method in viewmodel which fills the Processes. In my view I have structure something like, I have a treeview which will be bound to Processes and while selecting any of the item from the treeview, i need to allow user to edit that entity.

In the view the combo box binding is as:

<ComboBox ItemsSource="{Binding Path=Folders, Mode=OneWay}"
          DisplayMemberPath="FolderName" 
          SelectedItem="{Binding  Source={StaticResource viewModel}, Path=CurrentProcess.Folder, Mode=TwoWay}">
...

This binding doesn't work I mean when I select any object from the tree it fills other information like ProcesName in the textBox but it doesn't make the Folder object as the selected item in combobox, however the combo box will be filled.

Any suggestion.

Do refer this:

如果要通过编辑支持以两种方式将ComboBox绑定到folders属性,则应为组合框定义数据模板,然后将FolderInfo类的属性绑定到那些文本框绑定显示成员路径将无法解决您的问题

I'd suggest you to change DisplayMemberPath with appropriate DataTemplate :

<DataTemplate>
  <StackPanel>
     <TextBlock Text="{Binding FolderName}">
  </StackPanel>
<DataTemplate>

This will fix SelectedItem context.

Maybe just maybe, your Folderinfo class is not a notificationObject. If it is the case make sure it implements INotifyPropertyChange.

you have to use SelectedValuePath and SelectedValue instead of SelectedItem like below,

<ComboBox ItemsSource="{Binding Path=Folders, Mode=OneWay}"
          DisplayMemberPath="FolderName"
          SelectedValuePath="FolderId" 
          SelectedValue="{Binding Path=FolderId, Mode=TwoWay}">

SelectedItem binds the whole object whereas SelectedValue binds only particular properties of the object.

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