Boman

Hi all

Just wondered if it is possible to make sections ( items for headlines ) in a listbox . By sections i meen devide the listbox into ect. years ( See the picture )

What I would like was to make the "deviders/Sectionheaders" not selectable in the listbox and apply perhaps a differnet style/datatemplate , is there a way to accomplis that

/Bo




Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Marlon Grech

What you can do is using the grouping feature of WPF...

example

<ListBox>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border BorderBrush="Red">
<TextBlock="{Binding Path=PropertyToShow}"/>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>


hope this helps!!!





Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Marlon Grech

I forgot to tell you.... You need to add a groupDescription to the ICollectionView of the listbox...

if you need and more help please let me know






Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Boman

Thank you for the very quick reply Smile ..

but Iam sorry to say that I dont know how to use the IcollectionView of the listbox that you are talking about
( More designer than dev .. sorry )..
If you could enlighten me with a small example or throw a link I would be grateful

/Bo





Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Marlon Grech

hi there... this example should help...

http://www.beacosta.com/Archive/2006_01_01_bcosta_archive.html

So basically a CollectionViewSource is a view of the data that you are binding your control with...

I hope that this helps , if not feel free to get back to me





Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Boman

Great example Smile but my WPF-skills does not allow me to use the listbox as holder Sad

Get and error by doing so ..

------------------------------------------------------------

<ListBox ItemsSource="{Binding Source={StaticResource cvs}" ItemTemplate="{StaticResource }" >

<ListBox.GroupStyle>

<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />

</ListBox.GroupStyle>

</ListBox>

------------------------------------------------------------





Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Boman

forgot the animalTemplate ... But something is wrong

<ListBox ItemsSource="{Binding Source={StaticResource cvs}" ItemTemplate="{StaticResource animalTemplate}" >

<ListBox.GroupStyle>

<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />

</ListBox.GroupStyle>

</ListBox>





Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Boman

Got it Smile

Thank again Marlon for helping .. sometimes you just gotta know what to look for .

Another examlpe http://msdn2.microsoft.com/en-us/library/ms771725.aspx

Just merged the two exs . So used some off Beatriz's code
Ended up like this

------------------------------------------------------------------------------------------------

<Window.Resources>

<srcStick out tonguelaces x:Key="places"/>

<CollectionViewSource Source="{StaticResource places}" x:Key="cvs">

<CollectionViewSource.SortDescriptions>

<scmTongue TiedortDescription PropertyName="CityName"/>

</CollectionViewSource.SortDescriptions>

<CollectionViewSource.GroupDescriptions>

<datStick out tongueropertyGroupDescription PropertyName="State"/>

</CollectionViewSource.GroupDescriptions>

</CollectionViewSource>

<DataTemplate x:Key="categoryTemplate">

<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="12" Foreground="Tomato" Margin="0,0,0,0"/>

</DataTemplate>

<DataTemplate x:Key="animalTemplate">

<TextBlock Text="{Binding Path=CityName}" Foreground="Black" FontSize="40" Margin="30,0,0,0" />

</DataTemplate>

</Window.Resources>

------------------------------------------------------------------------------------------------

<DockPanel>

<ListBox FontSize="40" ItemsSource="{Binding Source={StaticResource cvs}}" Name="lb" ItemTemplate="{StaticResource animalTemplate}">

<ListBox.GroupStyle>

<!--<xTongue Tiedtatic Member="GroupStyle.Default" />-->

<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />

</ListBox.GroupStyle>

</ListBox>

</DockPanel>

</Window>







Re: Windows Presentation Foundation (WPF) "sections" in a listbox

Boman

Got it Smile

Thank again Marlon for helping .. sometimes you just gotta know what to look for .

Another examlpe http://msdn2.microsoft.com/en-us/library/ms771725.aspx

Just merged the two exs . So used some off Beatriz's code
Ended up like this

------------------------------------------------------------------------------------------------

<Window.Resources>

<srcStick out tonguelaces x:Key="places"/>

<CollectionViewSource Source="{StaticResource places}" x:Key="cvs">

<CollectionViewSource.SortDescriptions>

<scmTongue TiedortDescription PropertyName="CityName"/>

</CollectionViewSource.SortDescriptions>

<CollectionViewSource.GroupDescriptions>

<datStick out tongueropertyGroupDescription PropertyName="State"/>

</CollectionViewSource.GroupDescriptions>

</CollectionViewSource>

<DataTemplate x:Key="categoryTemplate">

<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="12" Foreground="Tomato" Margin="0,0,0,0"/>

</DataTemplate>

<DataTemplate x:Key="animalTemplate">

<TextBlock Text="{Binding Path=CityName}" Foreground="Black" FontSize="40" Margin="30,0,0,0" />

</DataTemplate>

</Window.Resources>

------------------------------------------------------------------------------------------------

<DockPanel>

<ListBox FontSize="40" ItemsSource="{Binding Source={StaticResource cvs}}" Name="lb" ItemTemplate="{StaticResource animalTemplate}">

<ListBox.GroupStyle>

<!--<xTongue Tiedtatic Member="GroupStyle.Default" />-->

<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />

</ListBox.GroupStyle>

</ListBox>

</DockPanel>

</Window>