繁体   English   中英

ComboBox TwoWay绑定不起作用

[英]ComboBox TwoWay binding not working

我总是完全被这些ComboBox所困扰。 我以为我了解他们,但似乎我不明白。

我将无法给对象父母。 所以我有了这个子对象,它的值是父代的ID,并且我有一个父代项的集合。

我从ComboBox中选择了Parent,如果我正确理解,它的ID属性应该绑定到Child的ParentId属性。 看起来不错,当我选择它时,属性结束。 模板已更改,并显示为文本块,一切正常。 当模板突然回到ComboBox类型时,它为Null。 它是否不应该在集合中找到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}" />

不知道您的完整设置是什么样的(我真的不能说出您的代码出了什么问题),但是我尝试对具有共同用途的相似模型进行建模,我制作了一个ListView绑定到一组具有Name的雇员和Occupation 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
}

暂无
暂无

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

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