0

具有动态列号的 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;

现在我想要一个带有复选框而不是文本的特殊行,我该怎么做?谷歌搜索示例,仅找到复选框列。

4

1 回答 1

0

嗨,这样做的逻辑是

    <Window.Resources>
    <DataGridRow x:Key="dr">
        <DataGridRow.Template>
            <ControlTemplate>
                <StackPanel Width="400" Orientation="Horizontal">
                <CheckBox Width="100"/>
                <CheckBox Width="100"/>
                <CheckBox Width="100"/>
                <CheckBox Width="100"/>
                </StackPanel>
            </ControlTemplate>
        </DataGridRow.Template>
    </DataGridRow>
</Window.Resources>
<Grid >
    <DataGrid AutoGenerateColumns="False" Width="400" CanUserAddRows="True" ItemsSource="{Binding PhoneNumbers}" LoadingRow="DataGrid_LoadingRow">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        if (e.Row.Item.ToString().Equals("{NewItemPlaceholder}"))
        {
            e.Row.Item = this.FindResource("dr");
            e.Row.DetailsVisibility = Visibility.Visible;
        }

    }

这不是确切的,但想法是一样的。我希望这个能帮上忙

于 2012-07-12T05:17:25.457 回答