简体   繁体   中英

Two way binding requires Path or XPath, DataGrid binding

I have a DataGrid that is to be filled in by the user, 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>

ViewModel:

InloopAangepast = new ObservableCollection<Inloop>();

And:

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

I can enter the first two fields (TypeOppervlak and TypeAfstroming) fine, but when I enter one of the other fields I get an error "Two way binding requires Path or XPath". What am I doing wrong?

You need to initialize list _parameters so that user can edit them. Initialize it in constructor:

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());
}

You need to add items in the list equal to the index you are binding on UI (7 items to be precise) .

Parameters property is null. Make sure to assign collection to the property in the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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