gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:29:55 28 4
gpt4 key购买 nike

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

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

绑定(bind)错误发生在RemoveRowGroup()之后当子 Property 对象从 PropertyGroup 的ObservableCollection 中移除时,为 PropertyGroup 调用。

似乎将DataGrid 的 单元格绑定(bind)到Property.Value 的BindingExpressions 仍在尝试在对象从DataGrid .

有什么想法吗?

最佳答案

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

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:我英语不好

关于wpf - 当 BindingExpression 使用数组索引时,已删除对象的 DataGrid 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529440/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com