2011-01-11 126 views
2

我总是被这些ComboBoxes完全运行。我想我理解他们,但似乎我不知道。ComboBox双向绑定不起作用

我不能给一个对象一个父母。所以我有这个子对象,它有一个父值的ID,我有一个父项的集合。

我从ComboBox中选择Parent,如果我理解正确,它的ID属性应该绑定到Child的ParentId属性。这看起来很好,当我选择它的财产结束。模板已更改,并且显示为文本块,一切正常。当模板突然回到组合框类型时,它是空的。它不应该在集合中找到Id对应于ParentId的可比项目吗?

下面是代码:

家长

public class Parent 
{ 
    private string _id; 
    public string Id 
    { 
     get 
     { 
      return _id; 
     } 
     set 
     { 
      _id = value; 
      OnPropertyChanged("Id"); 
     } 
    } 

    private string _name; 
    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      _name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 
} 

儿童

public class RulesMainClassViewModel : ViewModelBase 
{ 
    private string _id; 
    public string Id 
    { 
     get 
     { 
      return _id; 
     } 
     set 
     { 
      _id = value; 
      OnPropertyChanged("Id"); 
     } 
    } 

     private string _parentId; 
    public string ParentId 
    { 
     get 
     { 
      return _parentId; 
     } 
     set 
     { 
      _parentId = value; 
      OnPropertyChanged("ParentId"); 
     } 
    } 

    private string _name; 
    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      _name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 
} 

XAML组合框

<ComboBox DisplayMemberPath="Name" SelectedValue="{Binding Path=ParentId, Mode=TwoWay}" 
SelectedValuePath="Id" ItemsSource="{Binding Path=ParentCollection}" /> 
+0

这是WPF中的错误它称为级联组合框的问题,如果你仔细注意,当itemssource发生变化时,不幸的是,组合框将SelectedIndex设置为-1,这会导致SelectedValue清除并松开其绑定。 – 2011-01-11 19:53:06

+0

@Akash就是这样吗?这是在.Net中已经修复的东西4.我不相信ItemsSource在我的例子中已经改变。 – 2011-01-21 09:38:10

回答

0

不知道你完整的设置是什么样子(我可以真的不会告诉w帽子与你的代码错了),但我试图模拟类似的东西有一个共同的用途,我做了一个ListView绑定到一个员工的集合NameOccupation。该ComboBox具有改变选定员工的Occupation的目的,它的XAML看起来像这样:

<ComboBox ItemsSource="{Binding Occupations}" 
       SelectedValue="{Binding ElementName=lv,Path=SelectedItem.Occupation}" 
       SelectedValuePath="{Binding Occupation.Title}"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Title}"/> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

这似乎是工作。请注意,我SelectedValuePath是有约束力的,也许这是一个问题......


(类代码:)

public class Employee : INotifyPropertyChanged 
{ 
    private string name; 
    public string Name 
    { 
     get { return name; } 
     set { name = value; 
     NotifyPropertyChanged("Name");} 
    } 

    private Occupation occupation; 
    public Occupation Occupation 
    { 
     get { return occupation; } 
     set { occupation = value; 
     NotifyPropertyChanged("Occupation");} 
    } 

    public Employee(string name, Occupation occupation) 
    { 
     this.name = name; 
     this.occupation = occupation; 
    } 

    #region INotifyPropertyChanged Members 
    ... 
    #endregion 
} 

public class Occupation : INotifyPropertyChanged 
{ 
    private string title; 
    public string Title 
    { 
     get { return title; } 
     set { title = value; 
     NotifyPropertyChanged("Title");} 
    } 

    public Occupation(string title) 
    { Title = title; } 

    #region INotifyPropertyChanged Members 
    ... 
    #endregion 
}