2016-10-03 59 views
0

我正在使用wpf数据网格,并且使用分组来按顺序编号对我的订单进行分组,并且每个订单项目也有一个状态,例如:是继续还是不继续,但是不知何故,如果我列出每个项目的每个状态,它会在屏幕上显得杂乱,因为如果每个订单的一个项目都继续进行,这意味着所有项目也都继续进行,所以我想知道是否可以移动状态旁边的订单号码(扩展器头 - DockPanel),所以我可能看起来像这样:WPF DataGrid Dock面板分组

订单号:#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

Expander.Header没有将您的某个查看模型作为DataContext。取而代之的是头部获得一个从CollectionViewGroup继承的对象。其中一个属性是Name。这就是为什么你可以在你的XAML

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

感兴趣的另一个特性是Items绑定到Name。这是该组所有视图模型的列表。现在,它很容易在标题中访问某个项目的属性

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

感谢的解释,但我怎么能让它像的订单编号:#3 - 在流程,像我想保持的订单状态和数量TextBlock文本,acctualy我想显示两个属性number of orrders + status作为我的文本块的文本 –

+0

随意在任何其他位置创建和设置结果文本的样式。我会在你的案例中将两个'TextBlock'结合在水平对齐的'StackPanel'中(而不是你已经拥有的)。 – gomi42