2016-12-25 3 views
0

Я бы хотел показать всплывающее окно, чтобы помочь пользователям добавить новую строку в datagrid, добавив кнопку в последнюю строку datagrid. Я решил, что должно быть какое-то свойство DataGridTemplateColumn, которое я должен установить для шаблона newrow/addrow?Как добавить кнопку в последнюю/новую строку datagrid?

+0

Я предлагаю разместить эту кнопку где-то рядом с DataGrid, а не внутри нее. Это сделает его намного проще и чище. – icebat

ответ

1

Что вы можете сделать, это написать стиль для «DataGridRow», как

Thi является рабочим примером

<Style x:Type="DataGridRow"> 
<Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type DataGridRow}"> 
        <Grid Background="{TemplateBinding Background}"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="auto"/> 
         </Grid.ColumnDefinitions> 
         <ContentPresenter Grid.Column="0" /> 
         <Button Visibility = "{Binding IsLastRow}"/>!--can set converter to convert boolean to visibilty as well. 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
</Style> 

Так переплетено свойство в основном проверить логику последней строки. (Я не уверен, что wpf datagrid сам по себе предоставляет какое-то свойство типа «IsLastRow» или нет. Но если не в любое время, вы можете написать свою собственную логику).

В ContentTemplate вы можете определить обработчик или объект команды.

0

Вы можете использовать стиль DataGridRow с триггером данных, который изменяет шаблон последней строки:

<DataGrid x:Name="dgrid"> 
    <DataGrid.Resources> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding}" Value="{x:Static CollectionView.NewItemPlaceholder}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type DataGridRow}"> 
           <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" 
               BorderThickness="{TemplateBinding BorderThickness}" 
               Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
            <Grid> 
             <SelectiveScrollingGrid> 
              <SelectiveScrollingGrid.ColumnDefinitions> 
               <ColumnDefinition Width="Auto"/> 
               <ColumnDefinition Width="*"/> 
              </SelectiveScrollingGrid.ColumnDefinitions> 
              <SelectiveScrollingGrid.RowDefinitions> 
               <RowDefinition Height="*"/> 
               <RowDefinition Height="Auto"/> 
              </SelectiveScrollingGrid.RowDefinitions> 
              <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
              <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/> 
              <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> 
             </SelectiveScrollingGrid> 
             <Button Content="Add New" /> 
            </Grid> 
           </Border> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.Resources> 
</DataGrid> 

Или - если вы хотите поместить кнопку в определенном столбце - вы можете использовать DataGridTemplateColumn с CellTemplate, который использует аналогичный триггер данных:

Смежные вопросы