WPF DataGrid Dock面板分组

时间:2016-10-03 09:49:06

标签: c# wpf datagrid grouping dockpanel

我正在使用wpf datagrid,我正在使用分组按订单数量对我的订单进行分组,并且我还有每个订单商品的状态,例如:是否继续,但不知何故,它看起来很麻烦屏幕,如果我列出每个项目的每个状态,因为如果每个订单的一个项目继续,这意味着所有项目也在继续,所以我想知道是否可以移动订单号旁边的状态(扩展器头 - DockPanel)所以我可能看起来像这样:

  

订单号:#1 - 订单正在进行中。

  

订单号:#2 - 订单正在进行中。

  

订单号:#3 - 订单未在进行中。

enter image description here

所以问题是: 是否有可能在订单编号部分之后移动“订购状态”?:)

这是我的代码:

<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="btnFinishOrder" Content="Finish order" Margin="0,0,55,5" DockPanel.Dock="Right" Click="btnFinishOrder_Click" FontSize="12" BorderThickness="1.5" HorizontalAlignment="Left"  VerticalAlignment="Bottom"  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"  Foreground="#83D744"  Background="Transparent"  BorderBrush="#83D744" Width="130"   Height="40">
                                                    <Button.Template>
                                                        <ControlTemplate TargetType="Button">
                                                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                                            BorderBrush="{TemplateBinding BorderBrush}"
                                                            Background="{TemplateBinding Background}">
                                                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                            </Border>
                                                        </ControlTemplate>
                                                    </Button.Template>
                                                </Button>

                                                <Button Name="btnTakeIt" Click="btnTakeIt_Click"  Content="Take it!" Margin="0,0,20,5" DockPanel.Dock="Right" FontSize="12" BorderThickness="1.5" HorizontalAlignment="Left"  VerticalAlignment="Bottom"  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"  Foreground="#83D744"  Background="Transparent"  BorderBrush="#83D744" Width="130"   Height="40">
                                                    <Button.Template>
                                                        <ControlTemplate TargetType="Button">
                                                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                                            BorderBrush="{TemplateBinding BorderBrush}"
                                                            Background="{TemplateBinding Background}">
                                                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                            </Border>
                                                        </ControlTemplate>
                                                    </Button.Template>
                                                </Button>
                                                <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>
</DataGrid>

代码背后:

 public partial class MainWindow : Window
{
    CollectionViewSource collectionViewSource = new CollectionViewSource();

    public MainWindow()
    {
      try
        {


            InitializeComponent();


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

            var ordersList = OrdersController.localOrders();

            collectionViewSource.Source = ordersList;
            collectionViewSource.GroupDescriptions.Add(new PropertyGroupDescription("NumberOfOrder"));
            DataContext = collectionViewSource;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1000);
            timer.Tick += timer_Tick;
            timer.Start();

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

    }
}

1 个答案:

答案 0 :(得分:1)

Expander.Header未将您的某个视图模型设为DataContext。相反,标头获取一个继承自CollectionViewGroup的对象。其中一个属性是Name。这就是为什么你可以绑定到XAML中的Name

<TextBlock ... Text="{Binding Path=Name,StringFormat= Order Number:# {0}}" />

感兴趣的另一个属性是Items。这是该组所有视图模型的列表。现在,您可以轻松访问标题

中的项目属性
 <TextBlock ... Text="{Binding Path=Items[0].MyProperty}" />