2016-05-04 6 views
2

私は試合のライブ結果を得ることを可能にするアプリケーションを作った。 今、各試合の組織はこれです:複数のGroupDescriptionを挿入するにはどうすればよいですか?

Nation->League->Match list 

だから私はLeagueのコンテナをされ、そしてLeagueMatchのコンテナであるNationをしました。あなたが見ることができますどのように

<Window x:Class="GroupBox_Header.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:GroupBox_Header" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid Margin="10"> 
    <ListView Name="lvUsers"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Home Team" Width="50" DisplayMemberBinding="{Binding HomeTeam}" /> 
       <GridViewColumn Header="Away Team" Width="50" DisplayMemberBinding="{Binding AwayTeam}" /> 
      </GridView> 
     </ListView.View> 

     <ListView.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <Expander IsExpanded="True"> 
             <Expander.Header> 
              <StackPanel Orientation="Horizontal"> 
               <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 
               <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" /> 
               <TextBlock Text=" Items" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" /> 
              </StackPanel> 
             </Expander.Header> 
             <ItemsPresenter /> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
      <GroupStyle> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <Expander IsExpanded="True"> 
             <Expander.Header> 
              <StackPanel Orientation="Horizontal"> 
               <TextBlock Text="{Binding League}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 
              </StackPanel> 
             </Expander.Header> 
             <ItemsPresenter /> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </ListView.GroupStyle> 
    </ListView> 
</Grid> 

が、この中で私が保存し、GroupStyle定義がListViewがあります:前に言ったように私が最初にやったことは、私は、コンテナの結果を達成することを可能にするXAML構造を作成しましたすべてNationです。デモソリューションでXAMLを試して、結果を確認することができます。

public struct League 
{ 
    public string Name { get; set; } 
} 

public struct Country 
{ 
    public string Name { get; set; } 
    public League League; 
} 

は非常に非常に簡単です、すべてのリーグを救うためLeague構造体や国家のためCountry構造体:

この後、私は私が値を格納できるようにする構造体を作成しました。私はこのようにCountryに付加価値化しました:

List<Country> items = new List<Country>(); 
      items.Add(new Country() { Name = "Italy", League = { Name = "Serie A"} }); 
      items.Add(new Country() { Name = "Italy", League = { Name = "Serie B" } }); 
      items.Add(new Country() { Name = "England", League = { Name = "Premiere League" } }); 
      items.Add(new Country() { Name = "Spain", League = { Name = "Primeira Division" } }); 
      lvUsers.ItemsSource = items; 

は、だから私は実際にリストItaly (2), England, Spainを持っています。次にListView(XAMLコントロール)のItemsSourceitemsを挿入しました。 Nation名としてヘッダグループを作成し、私はCollectionViewを使用しましたために

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource); 
     PropertyGroupDescription groupNations = new PropertyGroupDescription("Name"); 

このコードは、私は複数のヘッダを作成することができ、その結果は、私がしたい正確に何です。また、私はNationコンテナ(ヘッダ)にsub-headerLeague名としてインサートである必要なものを起こす」、Leagueのために同じことをやった:

 view.GroupDescriptions.Add(groupNations); 
     PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League"); 
     view.GroupDescriptions.Add(groupNations); 

が、実際にこれが結果です:

enter image description here

あなたが唯一の国家ヘッダを見ることができますどのように

が表示されますが、LeagueヘッダはNationヘッダー内に表示されません。私は間違って何をしていますか?

+0

どのようにあなたは下になるのではなく、内側にあると確信していますか?あなたがネイションを閉じれば、それはなくなるのだろうか? – Paparazzi

+0

@パパラッチそれについてはわかりませんが、おそらく別の問題です。今はリーグの名前を印刷できません。 – Dillinger

答えて

2

あなたはLeagueプロパティを作ることによって、それを「修正」することができます(ゲッターとセッターを追加 - WPFのバインディングプロパティではなく、フィールドにアタッチする)だけではなく、"League""League.Name"することにより、第2グループの説明グループを作り、第2グループのスタイルを作るTextBlock結合はNameに結合し、Leagueには結合しない。

は、その後、あなたはこれを取得:

enter image description here

しかし、私はあなたがこの間違った方法を近づいて本当に奇妙な方法であなたのデータをモデル化していると言うでしょう。ListViewMatchesと表示される場合は、Matchオブジェクト(またはその上のビュー)のコレクションにバインドする必要があります。Matchはそれぞれ、親のLeagueを参照し、各Leagueは親のCountryを参照する必要があります。

はまた、リストの代わりに木を使用して、子供LeaguesのコレクションとCountry、および子Matchesのコレクションと各Leagueを持っています。

それともいる両方 - LeagueMatchesのコレクションが含まれますが、各試合は親League参照を持っている、とCountry/Leagueについても同様。

あなたのコードにある方法は奇妙なものです。

また、なぜクラスの代わりにオブジェクトに構造体を使用するのか分かりません。

変更の背後にあるコードする(注を、私は上記を参照、これはそれを行うには良い方法であると言っておりません):XAMLへ

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     List<Country> items = new List<Country>(); 
     items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie A" } }); // Added "new League" 
     items.Add(new Country() { Name = "Italy", League = new League { Name = "Serie B" } }); 
     items.Add(new Country() { Name = "England", League = new League { Name = "Premiere League" } }); 
     items.Add(new Country() { Name = "Spain", League = new League { Name = "Primeira Division" } }); 
     lvUsers.ItemsSource = items; 

     CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource); 
     PropertyGroupDescription groupNations = new PropertyGroupDescription("Name"); 
     view.GroupDescriptions.Add(groupNations); 
     PropertyGroupDescription groupCompetions = new PropertyGroupDescription("League.Name"); // Changed "League" to "League.Name" 
     view.GroupDescriptions.Add(groupCompetions); // Fixed the variable name here 

     lvUsers.ItemsSource = view; // Added, you probably have it but didn't post it 
    } 
} 

public struct League 
{ 
    public string Name { get; set; } 
} 

public struct Country 
{ 
    public string Name { get; set; } 
    public League League { get; set; } // added getter and setter 
} 

変更、ちょうど1行:

<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 

{Binding Name}は元のコードで{Binding League}です。

+0

新しいXAML 'GroupStyle'も追加しましたか?変更されたコードを表示できますか?私は将来、構造体をクラスに置き換えると思います。しかし、なぜあなたは構造体のアドバイスをしていませんか?構造体は、クラスと比較して軽量なデータの一種です。結果は次のようになります:http://i.stack.imgur.com/JuFRz.png私が追加したのはリーグだけです。 – Dillinger

+0

@Dillinger私はコードを追加しました...しかし、これは最初の場所でそれを行うには良い方法ではない、これはなぜ私はもともとそれを追加していないです。それは「ハック」です。 – svinja

+0

これに感謝します、代わりに何を提案しますか?私はvb.netとWindowsフォームでこのカスタムコントロールを作成しました:http://pastebin.com/phrjz2unしかし、これを再利用することはできません。 – Dillinger

関連する問題