2017-07-04 4 views
0

私のWPFアプリケーションの最初のヘッダの(背景)の色を変更し、私はこのリストビューを持っている: - のみ最初 - ヘッダーC#/ XAML - ListViewのグループ化:のみ

<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=" Ergebnis(se)" FontSize="22" 
           Foreground="Silver" FontStyle="Italic" 
           VerticalAlignment="Bottom"/> 
        </StackPanel> 
       </Expander.Header> 
       <ItemsPresenter/> 
       </Expander> 
      </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     </Style> 
    </GroupStyle.ContainerStyle> 
    </GroupStyle> 
</ListView.GroupStyle> 

は今、私が最初にしたいですそれ以外の色(ヘッダーの他の部分(フォント色または背景色、私は気にしません)にマーキングするために表示されます。 私はそれを行う方法についてのアイデアが浮かんできていません。

+0

あなたはすでに異なる色のヘッダー内にテキストを表示していますが、ここで何を達成したいですか?ヘッダーの背景をカラーにしたいですか? –

+0

いいえ最初のヘッダーのみを他のヘッダーとは異なる色で表示します。 – murkr

+0

「ヘッダー」とはどういう意味ですか?最初のエクスパンダーの背景を変更したいですか? – mm8

答えて

1

あなただけの最初のグループのための具体的なBrushを返すコンバータクラスを使用することができます。

public class GroupConverter : IValueConverter 
{ 
    int n; 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return n++ == 0 ? Brushes.Yellow : Brushes.Transparent; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML:それはのために実行されるようWPFで

<ListView x:Name="lv"> 
    <ListView.Resources> 
     <local:GroupConverter x:Key="converter" /> 
    </ListView.Resources> 
    <ListView.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <Expander IsExpanded="True" Background="{Binding Path=., Converter={StaticResource converter}}"> 
            <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=" Ergebnis(se)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" /> 
             </StackPanel> 
            </Expander.Header> 
            <Grid Background="White"> 
             <ItemsPresenter /> 
            </Grid> 
           </Expander> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </GroupStyle.ContainerStyle> 
     </GroupStyle> 
    </ListView.GroupStyle> 
</ListView> 
+0

リスト内のすべての項目に対してコンバーターが実行されるので、これは良い解決策ではありません –

+0

もちろんです。データトリガーと他の何かがそうです。これはItemsControlとループが動作する方法です。templateとGroupStyleは常にすべての項目に適用されます。 – mm8

+0

彼は常に最初のアイテムを必要とするので、直接ItemContainerGeneratorを通して達成します –

0

Converterは非常に高価ですそれぞれListViewItem。要件は最初の項目に常にスタイルを適用することであるため、はソリューションに最適です。

var expander = GetElement<Expander>(myListView); 
// Do expander style changes 

以下は、要素をビジュアルツリーから取得する一般的な方法です。

public T GetElement<T>(Visual element) 
{ 
     T foundElement = default(T); 
     if (element == null) return foundElement; 
     if (element.GetType() == typeof(T)) return (T)Convert.ChangeType(element, typeof(T)); 

     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) 
     { 
      Visual visual = VisualTreeHelper.GetChild(element, i) as Visual; 
      foundElement = GetElement<T>(visual); 
      if (foundElement != null) break; 
     } 
     return foundElement; 
    } 
関連する問題