双向绑定需要Path或XPath,DataGrid绑定

时间:2014-10-04 11:44:00

标签: c# wpf data-binding datagrid

我有一个要由用户填写的DataGrid,XAML:

<DataGrid ItemsSource="{Binding Path=Input.InloopAangepast, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=TypeOppervlak}"/>
        <DataGridTextColumn Binding="{Binding Path=TypeAfstroming}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[0].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[1].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[2].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[3].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[4].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[5].Double}"/>
        <DataGridTextColumn Binding="{Binding Path=Parameters[6].Double}"/>
    </DataGrid.Columns>
</DataGrid>

视图模型:

InloopAangepast = new ObservableCollection<Inloop>();

并且:

public class Inloop : INotifyPropertyChanged
{
    public Inloop()
    {
        Parameters =  new ObservableCollection<InloopParameter>();
    }

    private string _typeOppervlak;
    private string _typeAfstroming;
    private string _omschrijving;
    public ObservableCollection<InloopParameter> _parameters;

    public string TypeOppervlak
    {
        get
        {
            return _typeOppervlak;
        }
        set
        {
            _typeOppervlak = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("TypeOppervlak"));
            }
        }
    }

    public string TypeAfstroming
    {
        get
        {
            return _typeAfstroming;
        }
        set
        {
            _typeAfstroming = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("TypeAfstroming"));
            }
        }
    }

    public string Omschrijving
    {
        get
        {
            return _omschrijving;
        }
        set
        {
            _omschrijving = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Omschrijving"));
            }
        }
    }

    public ObservableCollection<InloopParameter> Parameters
    {
        get
        {
            return _parameters;
        }
        set
        {
            _parameters = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Parameters"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class InloopParameter : INotifyPropertyChanged
{
    public InloopParameter()
    {

    }

    private string _naam;
    private string _string;
    private double? _double;

    public string Naam
    {
        get
        {
            return _naam;
        }
        set
        {
            _naam = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Naam"));
            }
        }
    }

    public string String
    {
        get
        {
            return _string;
        }
        set
        {
            _string = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("String"));
            }
        }
    }

    public double? Double
    {
        get
        {
            return _double;
        }
        set
        {
            _double = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Double"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我可以输入前两个字段(TypeOppervlak和TypeAfstroming),但是当我输入其他字段之一时出现错误&#34;双向绑定需要Path或XPath&#34;。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您需要初始化列表 _parameters ,以便用户可以对其进行编辑。在构造函数中初始化它:

public Inloop()
{
    _parameters = new ObservableCollection<InloopParameter>();
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
    _parameters.Add(new InloopParameter());
}

您需要在列表中添加等于您在UI 上绑定的索引的项目(准确地说是7项)

答案 1 :(得分:0)

Parameters属性为null。确保将集合分配给构造函数中的属性。