0
我有一个绑定问题。我想将列表项绑定到datagrid列。 datagrid中的一行代表一个项目列表。每个项目都在一列中。例如,从列表中的所有项目索引为0的列与索引0将列表项绑定到数据网格
我的数据网格:
<DataGrid Name="AttacksForGroupsDG"
CanUserAddRows="False"
CanUserDeleteRows="True"
AutoGenerateColumns="False"
DockPanel.Dock="Top"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Height="250">
<DataGrid.Columns>
<DataGridTextColumn Header="Jméno vesnice" Binding="{Binding Name}" Width="120"/>
<DataGridTextColumn Header="Souřadnice" Binding="{Binding Coords}" Width="70"/>
</DataGrid.Columns>
</DataGrid>
接下来,我要,我结合项目的列表。此列表中的每一项代表我的数据网格中的一行。
public abstract class Group<T>
where T : Village
{
public ObservableCollection<T> Villages { get; private set; }
// the rest of the code I deleted for a clarity
}
最后,我绑定到这个类。从类Village继承的属性Name和Coords。
public sealed class Def : Village
{
public List<int> Attacks { get; set; }
public Def()
: base()
{
Attacks = new List<int>();
}
}
因此,datagrid中的第一列将与Name属性一起使用。第二列将与Coords属性一起。根据列表中项目的数量,我在代码中动态创建的下列列表Def中的攻击。
我不知道如何将该列表中的项目绑定到单个列。
我欣赏任何建议。
第2周的cols是固定的,那么就可以无限次列? – AnjumSKhan
@AnjumSKhan没错。我创建了XAML中的前两列以及我根据列表大小创建的其他列。 –
谷歌搜索:wpf datagrid变量列数 – AnjumSKhan