我当前有一个包含文本的DataGrid。我希望向其中一列添加动态自定义布局。布局将基于列表中的项数。但是,我不确定需要如何初始化自定义单元格或如何将其添加到单元格。
动态自定义单元格的外观应类似于以下内容:
foreach (var item in collection)
{
TextBox tb = new TextBox();
tb.Name = item.Name;
tb.ToolTip = item.Value;
}
然后需要添加到DataGrid中
<DataGrid x:Name="DataGridName">
<DataGrid.Columns>
<DataGridTemplateColumn Header="ColumnName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<"DYNAMIC CUSTOM CELL"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我已经搜索了周围,但我无法找到足够的信息来做这件事,所以帮助将不胜感激!
编辑:下面是有关底层代码如何:目前,数据网格如下所示:
<DataGrid x:Name="creatureDatagrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="maxHP" Binding="{Binding MaxHp}"/>
<DataGridTextColumn Header="AC" Binding="{Binding Ac}"/>
<DataGridTextColumn Header="INIT" Binding="{Binding Init}"/>
<DataGridTextColumn Header="HP" Binding="{Binding Hp}"/>
<DataGridTextColumn Header="Protection" Binding="{Binding StrProtection}"/>
<DataGridTextColumn Header="Speed" Binding="{Binding StrSpeed}"/>
<DataGridTextColumn Header="Senses" Binding="{Binding StrSenses}"/>
<DataGridTemplateColumn Header="Traits">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Traits[0].Name}" ToolTip="{Binding Traits[0].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[1].Name}" ToolTip="{Binding Traits[1].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[2].Name}" ToolTip="{Binding Traits[2].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[3].Name}" ToolTip="{Binding Traits[3].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[4].Name}" ToolTip="{Binding Traits[4].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[5].Name}" ToolTip="{Binding Traits[5].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[6].Name}" ToolTip="{Binding Traits[6].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[7].Name}" ToolTip="{Binding Traits[7].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[8].Name}" ToolTip="{Binding Traits[8].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[9].Name}" ToolTip="{Binding Traits[9].Value}" Margin="2,0"/>
<TextBlock Text="{Binding Traits[10].Name}" ToolTip="{Binding Traits[10].Value}" Margin="2,0"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Actions" Binding="{Binding StrActions}"/>
<DataGridTextColumn Header="Conditions" Binding="{Binding Conditions}"/>
</DataGrid.Columns>
</DataGrid>
底层代码如下:
public partial class MainWindow : Window
{
private readonly MonsterService _monsterService;
private readonly DatabaseService _databaseService;
public MainWindow()
{
InitializeComponent();
_monsterService = new MonsterService();
_databaseService= new DatabaseService();
foreach (var player in _databaseService.Players)
{
creatureDatagrid.Items.Add(player);
}
_databaseService.DoStuff();
foreach (var monster in _databaseService.ActiveMonsters)
{
creatureDatagrid.Items.Add(monster);
}
}
}
Players
和Monsters
被添加到CreatureDatagrid
。Players
和Monsters
是具有诸如Name
、HP
和Speed
的信息的类。
目前,我想为“Traits”创建一个自定义单元格。Monsters
包含list<NameValuePair>
,其中NameValuePair
包含Name
和Value
目前的Datagrid如下所示:
正如您所看到的,traits的Name
和Value
显示正确,但是代码并不是很漂亮...
1条答案
按热度按时间brjng4g31#
您可以在绑定到
Traits
属性的CellTemplate
中放置一个ItemsControl
: