WPF DataGrid - add button to end of every row

久未见 提交于 2019-12-23 22:42:55

问题


I'd like to add a button to the end of every row of my datagrid. I've found the following xaml but it adds the button to the start. Anyone know how I can add it after all the databound columns?

This adds the button to the start instead of the end:

  <DataGrid Background ="Black" ItemsSource="{Binding PriceList}">
    <DataGrid.Columns>
      <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <Button>My button</Button>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>

Many thanks in advance


回答1:


You'll have to add the columns manually.

Turn off the autogeneration of the columns and add them in the order you want them, including your extra column at the end of the list:

  <DataGrid Background ="Black"
            ItemsSource="{Binding PriceList}"
            AutoGenerateColumns="False">
    <DataGrid.Columns>

      <!-- Add your normal columns here -->

      <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <Button>My button</Button>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>



回答2:


Find the button control with some styles. The Style defines a Trigger element that changes the background property and visibility of a button based on button content/text.

 <DataGrid  ItemsSource="{Binding}" Name="dgvProcessLists" SelectionMode="Single">\
<DataGrid.Columns><DataGridTextColumn Binding="{Binding SIZE_FP}" FontFamily="Verdana" Header="SIZE FP" IsReadOnly="True" Width="100" />
<DataGridTemplateColumn Width="140" Header="Command">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="{Binding  DB_STEP_NAME}" Tag="{Binding STEP_ORDER}" Click="btnContinue_Click" >
                                        <Button.Style>
                                            <Style x:Name="ButtonVisibility">
                                                <Setter Property="Button.Visibility" Value="Hidden"/>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding STATUS}" Value="Failed">
                                                        <Setter Property="Button.Visibility" Value="Visible"/>
                                                        <Setter Property="Button.Background" Value="#777777"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding STATUS}" Value="Execute">
                                                        <Setter Property="Button.Visibility" Value="Visible"/>
                                                        <Setter Property="Button.Background" Value="AliceBlue"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding STATUS}" Value="Re-Evaluate">
                                                        <Setter Property="Button.Background" Value="Blue"/>
                                                    </DataTrigger>                                                    
                                                    <DataTrigger Binding="{Binding STATUS}" Value="Final">
                                                        <Setter Property="Button.Visibility" Value="Visible"/>
                                                        <Setter Property="Button.Background" Value="Blue"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Button.Style>
                                    </Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                            </DataGrid.Columns>
</Datagrid>



回答3:


If your DataGrid looks like this try following example:

 <DataGrid ItemsSource="{Binding PriceList}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"> 
                           <TextBlock Text="{Binding }"/>
                           <Button>My button</Button>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>


来源:https://stackoverflow.com/questions/25476423/wpf-datagrid-add-button-to-end-of-every-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!