简体   繁体   English

mvvm中的组合框绑定

[英]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) 我有一个看起来像(2个属性)的视图模型

    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 以下是ProcessMaintenanceDataObject定义

    [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. FolderInfo类具有FolderName和FolderId属性。

I have a method in viewmodel which fills the Processes. 我在viewmodel中有一个方法可以填充流程。 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. 这种绑定不起作用,是指当我从树中选择任何对象时,它会填充文本框内的其他信息(如ProcesName),但不会使Folder对象成为组合框中的选定项,但是组合框将被填充。

Any suggestion. 任何建议。

Do refer this: 请参考以下内容:

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

I'd suggest you to change DisplayMemberPath with appropriate DataTemplate : 我建议您使用适当的DataTemplate更改DisplayMemberPath

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

This will fix SelectedItem context. 这将修复SelectedItem上下文。

Maybe just maybe, your Folderinfo class is not a notificationObject. 也许只是,您的Folderinfo类不是NotificationObject。 If it is the case make sure it implements INotifyPropertyChange. 如果是这种情况,请确保它实现了INotifyPropertyChange。

you have to use SelectedValuePath and SelectedValue instead of SelectedItem like below, 您必须使用SelectedValuePath和SelectedValue而不是如下所示的SelectedItem,

<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. SelectedItem绑定整个对象,而SelectedValue仅绑定对象的特定属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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