如何从 DataGrid Wpf 上的 DockPanel 中获取价值 - 按钮单击

How to get value out of DockPanel on DataGrid Wpf - Button Click

我使用 DataGrid.Group 从数据库中对我的项目(订单产品)进行了分组,我将其分组为订单,所以它看起来像这样: 订单号:#{这里我放置了真实的来自数据库的 ID },例如订单号:#10,我现在想做的是将按钮放在停靠面板中包含的文本旁边,当我单击该按钮时我想对该订单执行一些操作,因为我已经在 header 中有了真实的订单 ID,我应该以某种方式 "take" 它,所以我可以在代码后面做类似的事情:删除订单,将其标记为继续或类似的东西那,这是它现在的样子,下面也是代码:

    <DataGrid.Columns >
        <DataGridTextColumn Binding="{Binding ProductName}"   ElementStyle="{StaticResource LeftAligElementStyle}"          Header="Product Name}" MinWidth="350"    Foreground="White" FontSize="20" FontFamily="Verdana" />
        <DataGridTextColumn Binding="{Binding Quantity}"        ElementStyle="{StaticResource LeftAligElementStyle}"     Header="Quantity"   MinWidth="200" Foreground="White"      FontSize="20" FontFamily="Verdana" />

    </DataGrid.Columns>

    <DataGrid.GroupStyle>
        <!-- Style for groups at top level. -->
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="True"  Background="Black" Opacity="0.7">
                                    <Expander.Header >
                                        <DockPanel Height="50" Margin="0,0,0,0"  Name="dockPanel" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=ActualWidth}">
                                            <Button Name="btnOrders" Content="Test" Margin="0,0,200,0" DockPanel.Dock="Right" />

                                            <TextBlock FontWeight="Normal" FontFamily="Verdana" FontSize="20" Height="25" Foreground="#83D744" Text="{Binding Path=Name,StringFormat= Order Number:# {0}}" />
                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsPresenter />
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>

后面的代码:

   public MainWindow()
    {
        try
        {


            InitializeComponent();


            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.WindowState = WindowState.Maximized;

            CollectionViewSource collectionViewSource = new CollectionViewSource();
            var ordersList = OrdesrController.localOrders();

            collectionViewSource.Source = ordersList;


            collectionViewSource.GroupDescriptions.Add(new PropertyGroupDescription("NumberOfOrder"));

            DataContext = collectionViewSource;




        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

处理 Button 的点击事件。

<Button Name="btnOrders" Click="Button_Click" .../>

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button b = sender as Button;

        CollectionViewGroup group = b.DataContext as CollectionViewGroup;
        /* make clever use of this group */
    }