1

我已经仔细阅读了该站点上的所有相关问题,但由于某种原因,我无法ListBox用两个 s 填充我的两个 es ObservableCollection。是我的绑定问题还是模型中的某个地方有问题?

模型:

public class DataModel : INotifyPropertyChanged
{
    public ObservableCollection<object> List1
    {
        get
        {
            return this.List1;
        }
        set
        {
            this.List1 = value;
            this.OnPropertyChanged("List1");
        }
    }

    public ObservableCollection<object> List2
    {
        get
        {
            return this.List2;
        }
        set
        {
            this.List2 = value;
            this.OnPropertyChanged("List2");
        }
    }

    public DataModel(ObservableCollection<object> _list1, ObservableCollection<object> _list2)
    {
        this.List1 = _list1;
        this.List2 = _list2;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML.cs:

public partial class UserControl1 : UserControl
{
    public ObservableCollection<object> l;
    public ObservableCollection<object> m;

    public UserControl1()
    {
        l = new ObservableCollection<object> { "test1a", "test1b" };
        m = new ObservableCollection<object> { "test2a", "test2b" };
        InitializeComponent();
        DataModel inst = new DataModel(l, m);
        this.DataContext = inst;
        this.TelProps.ItemsSource = l;
        this.SurProps.ItemsSource = m;
    }
}

XAML:

<Grid Name="Grid" DataContext="inst">
    <ListBox Name="FirstProps" 
             DataContext="{Binding Source=inst}"
             ItemsSource="{Binding List1}"
             DisplayMemberPath="List1" />
    <ListBox Name="SecondProps" 
             DataContext="{Binding Source=inst}"
             ItemsSource="{Binding List2}"
             DisplayMemberPath="List2" />
</Grid>
4

1 回答 1

3

您需要删除说明DisplayMemberPath符。这就是说要在 的每个项目上查找List1属性 。由于仅包含一组字符串,因此没有属性 on ,因此会出现绑定错误。List1List1List1System.String

于 2013-06-15T00:27:00.970 回答