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

时间:2016-09-29 17:22:52

标签: c# wpf xaml datagrid window

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

    <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);
        }
    }

1 个答案:

答案 0 :(得分:0)

处理按钮的点击事件。

<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 */
    }