Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've got a magical happy solution.</p> <p>You can create a custom Attached Property that you set in the Style, and upon setting it internally sets the GroupStyle property on the GridView.</p> <p>Attached Property:</p> <pre class="lang-cs prettyprint-override"><code>// Workaround for lack of generics in XAML public class GroupStyleCollection : Collection&lt;GroupStyle&gt; { } public class GroupStyleHelper { public static ICollection&lt;GroupStyle&gt; GetGroupStyle(ItemsControl obj) { return (ICollection&lt;GroupStyle&gt;)obj.GetValue(GroupStyleProperty); } public static void SetGroupStyle(ItemsControl obj, ICollection&lt;GroupStyle&gt; value) { obj.SetValue(GroupStyleProperty, value); } public static readonly DependencyProperty GroupStyleProperty = DependencyProperty.RegisterAttached( "GroupStyle", typeof(ICollection&lt;GroupStyle&gt;), typeof(GroupStyleHelper), new PropertyMetadata(null, OnGroupStyleChanged)); private static void OnGroupStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ItemsControl itemsControl = d as ItemsControl; if (itemsControl == null) return; RefreshGroupStyle(itemsControl, GetGroupStyle(itemsControl)); } private static void RefreshGroupStyle(ItemsControl itemsControl, IEnumerable&lt;GroupStyle&gt; groupStyle) { itemsControl.GroupStyle.Clear(); if (groupStyle == null) return; foreach (var item in groupStyle) { itemsControl.GroupStyle.Add(item); } } } </code></pre> <p>XAML Style:</p> <pre class="lang-xml prettyprint-override"><code> &lt;Style TargetType="ItemsControl"&gt; &lt;Setter Property="GroupStyleTest:GroupStyleHelper.GroupStyle"&gt; &lt;Setter.Value&gt; &lt;GroupStyleTest:GroupStyleCollection&gt; &lt;GroupStyle&gt; &lt;GroupStyle.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock FontWeight="Bold" FontSize="15" Text="{Binding Path=Name}" Foreground="HotPink"/&gt; &lt;/DataTemplate&gt; &lt;/GroupStyle.HeaderTemplate&gt; &lt;/GroupStyle&gt; &lt;/GroupStyleTest:GroupStyleCollection&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>Disclaimer: I'm testing this in WPF rather than WinRT but it should work the same, as far as I can tell. That's also why I'm using an ItemsControl rather than GridView, but the property is ItemsControl.GroupStyle anyway.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload