ComboBox TwoWay绑定无法正常工作

时间:2011-01-11 16:10:23

标签: c# wpf xaml combobox

我总是完全被这些ComboBoxs碾压。我想我理解他们,但似乎我没有。

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

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

以下是代码:

PARENT

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}" />

1 个答案:

答案 0 :(得分:0)

不确定你的完整设置是什么样的(我无法确定你的代码有什么问题)但是我试图模拟一些有共同用途的类似东西,我做了一个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
}