当BindingExpression使用数组索引时,已删除对象的DataGrid绑定错误

时间:2011-12-16 03:32:00

标签: wpf data-binding binding datagrid subscript

我正在尝试创建一个 DataGrid ,它是通过将 ItemsSource 属性设置为 PropertyGroup ObservableCollection 来填充的>每个 PropertyGroup 对象包含属性对象的 ObservableCollection 的对象。所有 PropertyGroups 都具有相同数量的Property Objects,因此我通过使用和数组下标的路径绑定它们。除了我从 DataGrid 中删除 PropertyGroup 对象后,我得到以下绑定错误,所以一切正常。

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'PropertyElement') 
from 'Children' (type 'ObservableCollection`1'). BindingExpression:Path=Children[3]
.Value; DataItem='PropertyGroupImpl' (HashCode=23661558); target element 
is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: 
Specified argument was out of the range of valid values.
Parameter name: index'

我的代码:

public class DataGridView : UserControl
{
    public DataGridView()
    {
        Rows = new ObservableCollection<PropertyGroup>();

        m_DataGrid = new DataGrid();
        m_DataGrid.AutoGenerateColumns = false;
        m_DataGrid.ItemsSource = Rows;
        Content = m_DataGrid;
    }

    public ObservableCollection<PropertyGroup> Rows { get; set; }

    public void AddRowGroup(PropertyGroup propertyGroup)
    {
        if(Rows.Count == 0)
            InitDataGrid(propertyGroup);

        Rows.Add(propertyGroup);
    }

    public void RemoveRowGroup(PropertyGroup propertyGroup)
    {
        Rows.Remove(propertyGroup);
    }

    void InitDataGrid(PropertyGroup firstGroup)
    {
        for(int i = 0; i < firstGroup.Children.Count; ++i)
        {
            Property prop = firstGroup.Children[i] as Property;

            DataGridColumn dgCol = null;
            Binding bnd = new Binding();
            bnd.Path = new PropertyPath("Children[" + i + "].Value");

            if(prop.Type == Property.EnumType.eBool)
                dgCol = CreateBooleanColumn(bnd);
            else
                dgCol = CreateTextColumn(bnd, prop.Value.GetType());

            dgCol.Header = prop.Name;
            m_DataGrid.Columns.Add(dgCol);
        }
    }

    DataGridColumn CreateTextColumn(Binding bnd, Type propType)
    {
        var textCol = new DataGridTextColumn();

        // Styling code removed for brevity 

        textCol.Binding = bnd;
        return textCol;
    }

    DataGrid m_DataGrid;

    DataGridColumn CreateBooleanColumn(Binding bnd)
    {
        var chkBoxCol = new DataGridCheckBoxColumn();

        chkBoxCol.Binding = bnd;
        return chkBoxCol;
    }
}

public class PropertyGroup
{
    public PropertyGroup()
    {
        Children = new ObservableCollection<PropertyElement>();
    }

    public ObservableCollection<PropertyElement> Children { get; set; }
}

public class Property : PropertyElement
{
    public enum EnumType {eBool, eInt, eUInt, eFloat, eDouble, eString,
        eVector2, eVector3, eVector4, eEnum};

    public EnumType Type { get; set; }

    public object Value { get; set; }
}

public class PropertyElement
{
    public string Name { get; set; }
}

当从 PropertyGroup的 Property EM>。

似乎 BindingExpressions DataGrid的单元绑定到Property.Value仍然在从 DataGrid中删除对象后仍在尝试更新

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

如何在Source中分配没有索引的PropertyPath属性?

Binding bnd = new Binding();
bnd.Path = new PropertyPath("Children[" + i + "].Value");

=&GT;

Binding bnd = new Binding();
bnd.Source = firstGroup.Children[i];
bnd.Path = new PropertyPath("Value");
PS:我不擅长英语