Datagrid不显示行上的按钮

时间:2015-12-18 21:07:48

标签: c# wpf datagrid

我创建了一个如下所示的数据网格:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="40" />
        </Grid.RowDefinitions>

        <DataGrid Grid.Row="0" ColumnWidth="*" ItemsSource="{Binding Items}" CanUserAddRows="false"
                  CanUserResizeColumns="False" AutoGenerateColumns="False" CanUserResizeRows="False"
                  IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedItem}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Prefix" Binding="{Binding Prefix, Mode=OneWay}" IsReadOnly="true" Width="*" />

                <DataGridTemplateColumn Header="Show">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Button>Details1</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Edit" IsReadOnly="True">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Button>Details2</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Delete" IsReadOnly="True">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Button>Details3</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Grid.Row="1" Width="70" HorizontalAlignment="Right" Content="Add" FontSize="16" Margin="0,10,0,0"
                Command="{Binding Create}" />
    </Grid>

我的问题是,当我执行wpf应用程序时: enter image description here

我在数据网格行中看不到任何按钮,但是当我在列中单击时,按钮显示为: enter image description here

为什么呢?如您所见,在列编辑和删除中,按钮也会消失。如此开心。

1 个答案:

答案 0 :(得分:2)

您定义了CellEditingTemplate但忘记包含CellTemplate或者您可能只需要CellTemplate ??:

<DataGridTemplateColumn Header="Show">

  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Details1}" />
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>

  <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
           <Button>Details1</Button>
       </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>

</DataGridTemplateColumn>