具有动态列号的 datgrid,如下所示:
<DataGrid DataGridCell.Selected="DataGrid_GotFocus" EnableColumnVirtualization="true" EnableRowVirtualization="true" Name="dataGrid1" DockPanel.Dock="Top" AutoGenerateColumns="False"
Height="120" Width="Auto" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserAddRows="false" CanUserResizeRows="False" CanUserSortColumns="False" CellEditEnding="dataGrid1_CellEditEnding">
</DataGrid>
for (var i = datetime; i < datetime.AddDays(1); i += TimeSpan.FromHours(1))
{
var column = new DataGridTemplateColumn();
column.Header = (i.Hour + 1).ToString();
column.Width = 30;
column.CellTemplate = (DataTemplate)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><TextBlock Text='{Binding ValuesExtended[" + i.Hour + @"].value_nullable, UpdateSourceTrigger=PropertyChanged}'/></DataTemplate>"
)));
column.CellEditingTemplate = (DataTemplate)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><TextBox Text='{Binding ValuesExtended[" + i.Hour + @"].value_nullable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}'/></DataTemplate>"
)));
dataGrid1.Columns.Add(column);
}
dataGrid1.ItemSource = data;
现在我想要一个带有复选框而不是文本的特殊行,我该怎么做?谷歌搜索示例,仅找到复选框列。