开发者

ComboBox TwoWay binding not working

I'm always totally getting run over by these ComboBoxes. I'm thinking I understand them but it seems like I don't.

I wan't to be able to give an object a parent. So I've got this child object, it has a value that's a ID of the parent and I have a collection of parent items.

I select the Parent from the ComboBox and if I understand correctly it's ID property should be bound to the Child's ParentId property. It seems fine, when I select it the property goes over. The template is changed and it's displayed as a Textblock, all fine. when the template goes back into the ComboBox type suddenly it's Null. Shouldn't it find the comparable item in the collection where it's Id corresponds with ParentId ?

Here is the code:

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("Nam开发者_C百科e");
        }
    }
}

CHILD

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

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


Not sure what your complete setting is like (and i can't really tell what's wrong with your code) but i tried to model something similar that has a common use, i made a ListView which binds to a collection of employees which have a Name and Occupation. The ComboBox has the purpose of changing the Occupation of the selected employee, the XAML for it looks like this:

    <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>

This seems to work. Note that my SelectedValuePath is a binding, maybe that is a problem...


(Class codes:)

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜