3

我正在尝试创建一个DataGrid ,它通过将ItemsSource属性设置为PropertyGroup对象的ObservableCollection来填充,其中每个PropertyGroup对象都包含一个属性对象的ObservableCollection。所有的PropertyGroups都有相同数量的属性对象,所以我通过使用数组下标的路径绑定到它们。一切正常,除了在从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 的Children ObservableCollectionRemoveRowGroup()中删除子 Property 对象时,在调用PropertyGroup后发生绑定错误。

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

有任何想法吗?

4

1 回答 1

3

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

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

=>

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

PS:我英语不好

于 2013-08-19T08:55:26.857 回答