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