简体   繁体   English

WPF DataGrid Dock面板分组

[英]WPF DataGrid Dock panel grouping

I'm using wpf datagrid, and I'm using grouping to group my orders by number of order, and I also have a status for each order item, like : is it proceed or not, but somehow it looks messy on screen if I list each status for each item, because if one item of each order is proceed that means all of items are also proceed , so I'm wondering is it possible to move status next to Order number (Expander header - DockPanel) so I might get look like this: 我正在使用wpf datagrid,并且正在使用分组将订单按订单数量分组,并且每个订单项都有一个状态,例如:是否继续进行,但是如果我在屏幕上看起来有点乱列出每个项目的每个状态,因为如果每个订单中有一个项目正在进行,则意味着所有项目也都在进行 ,所以我想知道是否可以将状态移动到订单编号(Expander标头-DockPanel)旁边,这样我可能会看起来像这样:

Order number :# 1 - Order is in progress. 订单号:#1-订单正在处理中。


Order number :# 2 - Order is in progress. 订单号:#2-订单正在处理中。


Order number :# 3 - Order is not in progress. 订单号:#3-订单未进行。

在此处输入图片说明

So question is: IS IT POSSIBLE TO MOVE 'ORDER STATUS' NEXT TO Order Number part?:) 所以问题是:是否可以在订单号部分旁边移动“订单状态”?:)

Here is my code: 这是我的代码:

<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>

code behind: 后面的代码:

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

    }
}

The Expander.Header does not get one of your view models as DataContext . Expander.Header不会将您的视图模型之一作为DataContext Instead the header gets an object that inherits from CollectionViewGroup . 相反,标头获取一个从CollectionViewGroup继承的对象。 One of its properties is Name . 它的属性之一是Name That's why you can bind to Name in your XAML 这就是为什么您可以在XAML中绑定到Name的原因

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

Another property of interest is Items . 感兴趣的另一个属性是Items That's the list of all view models of that group. 这是该组的所有视图模型的列表。 Now it's easy to access an item's property in the header 现在很容易在标题中访问项目的属性

 <TextBlock ... Text="{Binding Path=Items[0].MyProperty}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM