ItemsSourceプロパティを、各PropertyGroupオブジェクトにプロパティ オブジェクトのObservableCollectionが含まれるPropertyGroupオブジェクトのObservableCollectionに設定することによって、入力されるDataGridを作成しようとしています。すべての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; }
}
子 Property オブジェクトがPropertyGroup のChildren ObservableCollectionから削除されているときに、 PropertyGroupRemoveRowGroup()
に対して が呼び出された後にバインディング エラーが発生します。
DataGrid のセルを Property.Value にバインドするBindingExpressionsは、オブジェクトがDataGridから削除された後も更新を試みているようです。
何か案は?