2012-02-09 17 views
2

プロパティに基づいてグループ化されたWPFでDataGridを取得しましたが、対応する項目がない場合でもグループを表示することはできません。WPFのDataGridで空のグループを持つには?

私のコードはthatに基づいていますが、DataGridでは動作しません。

<Window 
x:Class="EmptyGroups.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Servers by cluster" 
WindowStartupLocation="CenterScreen"> 

<DockPanel> 

    <WrapPanel DockPanel.Dock="Top" Background="BlanchedAlmond"> 
     <Label Content="Cluster name:" Margin="10"/> 
     <TextBox x:Name="NewClusterName" Text="type new cluster name here" MinWidth="50" BorderThickness="1" Margin="10"/> 
     <Button Content="Add cluster" Click="AddNewCluster_Click" Margin="10"/> 
    </WrapPanel> 

    <ListView x:Name="ServersList"> 

     <ListView.GroupStyle> 
      <GroupStyle HidesIfEmpty="False"> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="GroupItem"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="GroupItem"> 
            <Expander IsExpanded="True"> 
             <Expander.Header> 
              <TextBlock TextWrapping="Wrap" Margin="0,10,0,5" > 
               <Bold><TextBlock Text="{Binding Name}"/></Bold> (<TextBlock Text="{Binding ItemCount}"/> servers) 
              </TextBlock> 
             </Expander.Header> 
             <ItemsPresenter/> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </ListView.GroupStyle> 

     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Server" DisplayMemberBinding="{Binding Name}"/> 
       <GridViewColumn Header="Cluster" DisplayMemberBinding="{Binding Cluster.Name}"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 

    <DataGrid Name="ServersGrid" AutoGenerateColumns="False" IsReadOnly="True"> 
     <DataGrid.GroupStyle> 
      <GroupStyle HidesIfEmpty="False"> 
       <GroupStyle.Panel> 
        <ItemsPanelTemplate> 
         <DataGridRowsPresenter/> 
        </ItemsPanelTemplate> 
       </GroupStyle.Panel> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <DockPanel> 
          <TextBlock Text="" Width="20" /> 
          <TextBlock Text="{Binding Path=Name}"/> 
         </DockPanel> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
      </GroupStyle> 
     </DataGrid.GroupStyle> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="ServerGrid" Binding="{Binding Name}" /> 
      <DataGridTextColumn Header="ClusterGrid" Binding="{Binding Cluster.Name}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
</DockPanel> 

そしてWindows1.xaml.cs:6の後

using System.Collections.ObjectModel; 
using System.Windows; 
using System.Windows.Data; 

namespace EmptyGroups 
{ 
public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 

     var clusters = new[] 
     { 
      new Cluster { Name = "Front end" }, 
      new Cluster { Name = "Middle end" }, 
      new Cluster { Name = "Back end" }, 
     }; 

     var collectionView = new ListCollectionView(new[] 
     { 
      new Server { Cluster = clusters[0], Name = "webshop1" }, 
      new Server { Cluster = clusters[0], Name = "webshop2" }, 
      new Server { Cluster = clusters[0], Name = "webshop3" }, 
      new Server { Cluster = clusters[0], Name = "webshop4" }, 
      new Server { Cluster = clusters[0], Name = "webshop5" }, 
      new Server { Cluster = clusters[0], Name = "webshop6" }, 
      new Server { Cluster = clusters[2], Name = "sql1" }, 
      new Server { Cluster = clusters[2], Name = "sql2" }, 
     }); 

     var groupDescription = new PropertyGroupDescription("Cluster.Name"); 

     // this foreach must at least add clusters that can't be 
     // derived from items - i.e. groups with no items in them 
     foreach (var cluster in clusters) 
      groupDescription.GroupNames.Add(cluster.Name); 

     collectionView.GroupDescriptions.Add(groupDescription); 
     ServersList.ItemsSource = collectionView; 
     ServersGrid.ItemsSource = collectionView; 
     Clusters = groupDescription.GroupNames; 
    } 

    readonly ObservableCollection<object> Clusters; 

    void AddNewCluster_Click(object sender, RoutedEventArgs e) 
    { 
     Clusters.Add(NewClusterName.Text); 
    } 
} 

class Cluster 
{ 
    public string Name { get; set; } 
} 

class Server 
{ 
    public Cluster Cluster { get; set; } 
    public string Name { get; set; } 
} 
} 

すべての空のグループを表示するには、データグリッドストップを追加します。おかげさまで

+0

もっとコードをください –

+0

私はちょうどコードを追加しました、それはDataGridはアイテムよりも多くのグループを持つことはできないようです、そして、それはWPFバグのようです。 – hokkos

+1

その場合は、 "NullServer"や "EmptyCluster"を作成し、その値/タイプのカスタムスタイリング/テンプレートを定義します。 –

答えて

0

質問に続いて、私は自分自身に答えます。 WPF DataGridのバグのために、アイテム以外のグループは存在しないので、存在するすべてのグループにゴーストアイテムを追加し、Visibility as Collapsedを設定します。

関連する問題