<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How can I do custom Grouping?</title>
	<atom:link href="http://bea.stollnitz.com/blog/?feed=rss2&#038;p=19" rel="self" type="application/rss+xml" />
	<link>http://bea.stollnitz.com/blog/?p=19</link>
	<description>on Silverlight and WPF</description>
	<lastBuildDate>Thu, 22 Jul 2010 20:56:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-170245</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Fri, 20 Nov 2009 18:35:17 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-170245</guid>
		<description>Hi Chris,

The group converter&#039;s CultureInfo is taken directly from the view&#039;s Culture property. If you&#039;re using a CollectionViewSource and set its Culture property, it will set the Culture property of the view it creates to be the same as its own property. If you&#039;re not using a CollectionViewSource, you can set the Culture property of the view directly.

This behavior is a bit inconsistent compared to the Binding&#039;s converter, which always gets it culture from the target element&#039;s Language property. It seems to me that if the group converter fell back to the element&#039;s Language when the CVS&#039;s Culture is null, your scenario would work as you expected.

I wonder if getting the current culture through code (CultureInfo.CurrentCulture) in the converter would work for you. If not, could you set the CollectionViewSource&#039;s Culture?

Bea</description>
		<content:encoded><![CDATA[<p>Hi Chris,</p>
<p>The group converter&#8217;s CultureInfo is taken directly from the view&#8217;s Culture property. If you&#8217;re using a CollectionViewSource and set its Culture property, it will set the Culture property of the view it creates to be the same as its own property. If you&#8217;re not using a CollectionViewSource, you can set the Culture property of the view directly.</p>
<p>This behavior is a bit inconsistent compared to the Binding&#8217;s converter, which always gets it culture from the target element&#8217;s Language property. It seems to me that if the group converter fell back to the element&#8217;s Language when the CVS&#8217;s Culture is null, your scenario would work as you expected.</p>
<p>I wonder if getting the current culture through code (CultureInfo.CurrentCulture) in the converter would work for you. If not, could you set the CollectionViewSource&#8217;s Culture?</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-157691</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 29 Jul 2009 01:33:50 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-157691</guid>
		<description>Hi Bea.
I&#039;ve been looking over this code for grouping and sorting data in a treeview, it&#039;s been very helpful there seem to be very few examples of how to do this.
However I&#039;ve run into a stumbling block.  The converter for grouping the data seems to have a null CultureInfo.  This is an issue for me, as we are grouping Dates by months and need to display the localised name.
I was wondering if you knew if this was an issue with .NET, or there was something missing from the code?</description>
		<content:encoded><![CDATA[<p>Hi Bea.<br />
I&#8217;ve been looking over this code for grouping and sorting data in a treeview, it&#8217;s been very helpful there seem to be very few examples of how to do this.<br />
However I&#8217;ve run into a stumbling block.  The converter for grouping the data seems to have a null CultureInfo.  This is an issue for me, as we are grouping Dates by months and need to display the localised name.<br />
I was wondering if you knew if this was an issue with .NET, or there was something missing from the code?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-251</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Mon, 13 Aug 2007 21:28:02 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-251</guid>
		<description>Hi Doug,

I&#039;ve already followed up with your personally, but I will reply to your question here too for future reference.

TreeView has a property named ItemContainerStyleSelector which takes a StyleSelector object. This allows us to pick a Style for each item, based on whatever custom logic we want. In this case, the logic is simple, we simply need to check for the type of the data item, which we get as a parameter to the SelectStyle method: 

public class ContainerStyle : System.Windows.Controls.StyleSelector
{
(…)

public override Style SelectStyle(object item, DependencyObject container)
{
if(item is ClassA)
{
return styleClassA;
}
else if(item is ClassB)
{
return styleClassB;
}
return null;
}
} 

You can find &lt;a href=&quot;http://www.beacosta.com/BlogComments/25StyleSelector.zip&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt; a sample with this code.

I’ve seen people get stuck with this scenario because we need access to the styles, which are typically defined in the main Window. In this app I simply added a couple of public properties in the ContainerStyle class of type Style, and I set those properties to the styles I defined in XAML. There are probably different ways to do this.

Let me know if this helps and if you have any further questions.</description>
		<content:encoded><![CDATA[<p>Hi Doug,</p>
<p>I&#8217;ve already followed up with your personally, but I will reply to your question here too for future reference.</p>
<p>TreeView has a property named ItemContainerStyleSelector which takes a StyleSelector object. This allows us to pick a Style for each item, based on whatever custom logic we want. In this case, the logic is simple, we simply need to check for the type of the data item, which we get as a parameter to the SelectStyle method: </p>
<p>public class ContainerStyle : System.Windows.Controls.StyleSelector<br />
{<br />
(…)</p>
<p>public override Style SelectStyle(object item, DependencyObject container)<br />
{<br />
if(item is ClassA)<br />
{<br />
return styleClassA;<br />
}<br />
else if(item is ClassB)<br />
{<br />
return styleClassB;<br />
}<br />
return null;<br />
}<br />
} </p>
<p>You can find <a href="http://www.beacosta.com/BlogComments/25StyleSelector.zip" rel="nofollow">here</a> a sample with this code.</p>
<p>I’ve seen people get stuck with this scenario because we need access to the styles, which are typically defined in the main Window. In this app I simply added a couple of public properties in the ContainerStyle class of type Style, and I set those properties to the styles I defined in XAML. There are probably different ways to do this.</p>
<p>Let me know if this helps and if you have any further questions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Hagan</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-250</link>
		<dc:creator>Doug Hagan</dc:creator>
		<pubDate>Sun, 12 Aug 2007 14:55:20 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-250</guid>
		<description>Hi Beatriz,

Is there a way to specify styles for TreeViewItems based upon the Type of the TreeViewItem? For example, I want to have the Account expander and node to have a different style than the Campaign expander and node. Since I am stylizing the TreeViewItem type I&#039;m not sure how to break out the underlying data types.

Thanks for all of your helpful posts!

&lt;Style d:IsControlPart=&quot;True&quot; TargetType=&quot;{x:Type TreeViewItem}&quot;&gt;
.
.
.
&lt;/Style&gt;

&lt;src:CustomerDataList x:Key=&quot;MyList&quot;/&gt;
&lt;HierarchicalDataTemplate DataType = &quot;{x:Type src:CustomerData}&quot; ItemsSource = &quot;{Binding Path=AccountDataList}&quot; &gt;
&lt;TextBlock Text=&quot;{Binding Path=Name}&quot; /&gt;
&lt;/HierarchicalDataTemplate&gt;
&lt;HierarchicalDataTemplate DataType = &quot;{x:Type src:AccountData}&quot; ItemsSource = &quot;{Binding Path=CampaignDataList}&quot;&gt;
&lt;TextBlock Text=&quot;{Binding Path=Name}&quot; /&gt;
&lt;/HierarchicalDataTemplate&gt;</description>
		<content:encoded><![CDATA[<p>Hi Beatriz,</p>
<p>Is there a way to specify styles for TreeViewItems based upon the Type of the TreeViewItem? For example, I want to have the Account expander and node to have a different style than the Campaign expander and node. Since I am stylizing the TreeViewItem type I&#8217;m not sure how to break out the underlying data types.</p>
<p>Thanks for all of your helpful posts!</p>
<p>&lt;Style d:IsControlPart=&#8221;True&#8221; TargetType=&#8221;{x:Type TreeViewItem}&#8221;&gt;<br />
.<br />
.<br />
.<br />
&lt;/Style&gt;</p>
<p>&lt;src:CustomerDataList x:Key=&#8221;MyList&#8221;/&gt;<br />
&lt;HierarchicalDataTemplate DataType = &#8220;{x:Type src:CustomerData}&#8221; ItemsSource = &#8220;{Binding Path=AccountDataList}&#8221; &gt;<br />
&lt;TextBlock Text=&#8221;{Binding Path=Name}&#8221; /&gt;<br />
&lt;/HierarchicalDataTemplate&gt;<br />
&lt;HierarchicalDataTemplate DataType = &#8220;{x:Type src:AccountData}&#8221; ItemsSource = &#8220;{Binding Path=CampaignDataList}&#8221;&gt;<br />
&lt;TextBlock Text=&#8221;{Binding Path=Name}&#8221; /&gt;<br />
&lt;/HierarchicalDataTemplate&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-238</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Thu, 14 Sep 2006 19:30:53 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-238</guid>
		<description>Hi Jason,

You can set properties and register events on a generated ListBoxItem by defining a style for it. This can be done by setting the ItemContainerStyle property of ItemsControl to your style. 

There is a Setter element that can be used inside a Style to set properties on the element being styled. There is also an EventSetter element that can register event handlers for the events you specify. 

In the following markup, I am setting the foreground of all ListBoxItems to blue. I also have event handlers set up for MouseEnter/MouseLeave. 

 &lt;Style x:Key=&quot;ContainerStyle&quot; TargetType=&quot;{x:Type ListBoxItem}&quot;&gt;
   &lt;Style.Setters&gt;
     &lt;EventSetter Event=&quot;Mouse.MouseEnter&quot; Handler=&quot;MouseEnterHandler&quot;/&gt;
     &lt;EventSetter Event=&quot;Mouse.MouseLeave&quot; Handler=&quot;MouseLeaveHandler&quot;/&gt;
     &lt;Setter Property=&quot;Foreground&quot; Value=&quot;DarkBlue&quot; /&gt;
   &lt;/Style.Setters&gt;
 &lt;/Style&gt;

In the complete sample (see link below), I have event handlers such that when I mouse over the item the background becomes light blue, and when I mouse out the background goes back to white.

You can find a complete sample with this &lt;a href=&quot;http://www.beacosta.com/BlogComments/1MouseEnterListBoxItem.zip&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.

Let me know if this answers your question.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Jason,</p>
<p>You can set properties and register events on a generated ListBoxItem by defining a style for it. This can be done by setting the ItemContainerStyle property of ItemsControl to your style. </p>
<p>There is a Setter element that can be used inside a Style to set properties on the element being styled. There is also an EventSetter element that can register event handlers for the events you specify. </p>
<p>In the following markup, I am setting the foreground of all ListBoxItems to blue. I also have event handlers set up for MouseEnter/MouseLeave. </p>
<p> &lt;Style x:Key=&#8221;ContainerStyle&#8221; TargetType=&#8221;{x:Type ListBoxItem}&#8221;&gt;<br />
   &lt;Style.Setters&gt;<br />
     &lt;EventSetter Event=&#8221;Mouse.MouseEnter&#8221; Handler=&#8221;MouseEnterHandler&#8221;/&gt;<br />
     &lt;EventSetter Event=&#8221;Mouse.MouseLeave&#8221; Handler=&#8221;MouseLeaveHandler&#8221;/&gt;<br />
     &lt;Setter Property=&#8221;Foreground&#8221; Value=&#8221;DarkBlue&#8221; /&gt;<br />
   &lt;/Style.Setters&gt;<br />
 &lt;/Style&gt;</p>
<p>In the complete sample (see link below), I have event handlers such that when I mouse over the item the background becomes light blue, and when I mouse out the background goes back to white.</p>
<p>You can find a complete sample with this <a href="http://www.beacosta.com/BlogComments/1MouseEnterListBoxItem.zip" rel="nofollow">here</a>.</p>
<p>Let me know if this answers your question.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-249</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sat, 04 Mar 2006 11:34:46 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-249</guid>
		<description>Hi Shanty,

If you want to do custom grouping at the second level, you can simply define another group converter and add a second PropertyGroupDescription to the GroupDescriptions collection of the CollectionViewSource with that converter. I uploaded a project with the xaml I described &lt;a href=&quot;http://www.beacosta.com/BlogComments/21CustomMultiLevelGrouping.zip&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;. Let me know if this helps.

Sorry, my spanish is not that great. :) I speak european portuguese.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Shanty,</p>
<p>If you want to do custom grouping at the second level, you can simply define another group converter and add a second PropertyGroupDescription to the GroupDescriptions collection of the CollectionViewSource with that converter. I uploaded a project with the xaml I described <a href="http://www.beacosta.com/BlogComments/21CustomMultiLevelGrouping.zip" rel="nofollow">here</a>. Let me know if this helps.</p>
<p>Sorry, my spanish is not that great. :) I speak european portuguese.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shanty</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-248</link>
		<dc:creator>Shanty</dc:creator>
		<pubDate>Fri, 03 Mar 2006 04:45:38 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-248</guid>
		<description>Hi Bea (por cierto, por tu nombre, quizá eres de habla latina, no lo sé).
I&#039;m researching doin&#039; the same as you, but grouping at secnd level, not first. I think mixing between Convert and putting a HierarchicalDataTemplate before CollectionViewSource, I&#039;ll find the solution, but...
Am I in the correct way??

Regards</description>
		<content:encoded><![CDATA[<p>Hi Bea (por cierto, por tu nombre, quizá eres de habla latina, no lo sé).<br />
I&#8217;m researching doin&#8217; the same as you, but grouping at secnd level, not first. I think mixing between Convert and putting a HierarchicalDataTemplate before CollectionViewSource, I&#8217;ll find the solution, but&#8230;<br />
Am I in the correct way??</p>
<p>Regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-247</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Sat, 25 Feb 2006 14:07:57 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-247</guid>
		<description>Hi Sergey,

Thanks for your nice words.

You can control how the group name is displayed and what information you show, in the template for the group. In this particular sample, I am grabbing the template I want from the resources, in the &quot;SelectTemplate&quot; method. For group headers, I am picking the template with key &quot;GroupTemplate&quot;, so this is the template you should change to display ID only. You can do this by changing the Path of the Binding. If you look at the &quot;GroupTemplate&quot; in this sample, you will see the following binding: {Binding Path=Name}. Most likely you will simply need to change the Path to Path=ID.

Does this answer your question?

Bea</description>
		<content:encoded><![CDATA[<p>Hi Sergey,</p>
<p>Thanks for your nice words.</p>
<p>You can control how the group name is displayed and what information you show, in the template for the group. In this particular sample, I am grabbing the template I want from the resources, in the &#8220;SelectTemplate&#8221; method. For group headers, I am picking the template with key &#8220;GroupTemplate&#8221;, so this is the template you should change to display ID only. You can do this by changing the Path of the Binding. If you look at the &#8220;GroupTemplate&#8221; in this sample, you will see the following binding: {Binding Path=Name}. Most likely you will simply need to change the Path to Path=ID.</p>
<p>Does this answer your question?</p>
<p>Bea</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sergey</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-246</link>
		<dc:creator>sergey</dc:creator>
		<pubDate>Fri, 24 Feb 2006 09:51:20 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-246</guid>
		<description>Hi Beatriz! Blog is really wonderfull! I&#039;ll apreciate, if you answer one question. You wrote that: &quot;Notice also that the groups don&#039;t have to be a string, they can be any object you want.&quot;. The question is if return type of Convert() method is a custom class(for example it has two properties: Id and Name), how could id be displayed in the header of the group? Thanks.</description>
		<content:encoded><![CDATA[<p>Hi Beatriz! Blog is really wonderfull! I&#8217;ll apreciate, if you answer one question. You wrote that: &#8220;Notice also that the groups don&#8217;t have to be a string, they can be any object you want.&#8221;. The question is if return type of Convert() method is a custom class(for example it has two properties: Id and Name), how could id be displayed in the header of the group? Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bea</title>
		<link>http://bea.stollnitz.com/blog/?p=19&#038;cpage=1#comment-245</link>
		<dc:creator>Bea</dc:creator>
		<pubDate>Tue, 21 Feb 2006 19:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://bea.stollnitz.com/blog/?p=19#comment-245</guid>
		<description>Hi Jane,

I uploaded to my server a sample that does custom grouping with two levels of groups: first it groups by the first letter of the Greek God or Hero&#039;s name, and then it groups by type. You can find my sample here.

Let me know if this is what you were looking for.

Bea</description>
		<content:encoded><![CDATA[<p>Hi Jane,</p>
<p>I uploaded to my server a sample that does custom grouping with two levels of groups: first it groups by the first letter of the Greek God or Hero&#8217;s name, and then it groups by type. You can find my sample here.</p>
<p>Let me know if this is what you were looking for.</p>
<p>Bea</p>
]]></content:encoded>
	</item>
</channel>
</rss>
