Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:42:01+00:00 2026-06-04T19:42:01+00:00

I am new to both WPF and MVVM so I will beg forgiveness upfront

  • 0

I am new to both WPF and MVVM so I will beg forgiveness upfront if this is a silly question.

Problem:
I am trying to create a grouped list of items using the MVVM design pattern. I can do it with code behind, but would prefer a more elegant solution.

Details

  • Let’s say I have a collection of animals: horse, wolf, monkey, tiger, polar bear, zebra, bat, etc.
  • These animals are grouped by continents: North America, Africa, Antarctica, etc.

Goal: Within a wrap panel, I would like to create grouped toggle buttons. For example, there would be a “North America” GroupBox with ToggleButtons for each animal found in North America. Next, there would be a GroupBox with the header “Africa” and inside the group box would be all the animals in Africa.

Using the MVVM design pattern, I can bind to an ObservableCollection and, using a data template, create the toggle buttons I need. Where I’m struggling is I don’t know how to group the animals. All I need are guidelines to follow. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:42:02+00:00Added an answer on June 4, 2026 at 7:42 pm

    In the View, set the items source to a CollectionViewSource that itself binds to the Animals collection on the ViewModel. The CollectionViewSource can be grouped, something like this:

    <CollectionViewSource.GroupDescriptions>
       <PropertyGroupDescription PropertyName="Continent" />
    </CollectionViewSource.GroupDescriptions>
    

    You’ll also need to set a group style on whatever items control you’ve got – something like this

    <ItemsControl.GroupStyle>
       <GroupStyle>
          <GroupStyle.HeaderTemplate>
             <DataTemplate>
                <TextBlock FontWeight="Bold" FontSize="15"
                   Text="{Binding Path=Name}"/>
             </DataTemplate>
          </GroupStyle.HeaderTemplate>
       </GroupStyle>
    </ItemsControl.GroupStyle>
    

    Taken from the example on this page – http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.groupstyle.aspx

    That’s setting the HeaderTemplate, but if you play around a bit you should be able to set a container style for each group.

    Hope this helps!

    Update:
    I wasn’t too sure about this so I had a quick bash at the code. Assuming ‘toggle button’ is ‘radio button’, this is what I’ve come up with:

    <Grid>
        <Grid.Resources>
            <CollectionViewSource x:Key="Animals" Source="{Binding}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Continent" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Grid.Resources>
    
        <ItemsControl ItemsSource="{Binding Source={StaticResource Animals}}">
            <ItemsControl.GroupStyle>
                <x:Static Member="GroupStyle.Default" />
            </ItemsControl.GroupStyle>
    
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding Name}" GroupName="{Binding Continent}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    

    In addition, you can display each group as a GroupBox by replacing the line <x:Static Member="GroupStyle.Default" /> with:

    <GroupStyle>
       <GroupStyle.ContainerStyle>
          <Style TargetType="{x:Type GroupItem}">
             <Setter Property="Template">
                <Setter.Value>
                   <ControlTemplate>
                      <GroupBox Margin="10" Header="{Binding Name}">
                         <GroupBox.Content>
                            <ItemsPresenter />
                         </GroupBox.Content>
                      </GroupBox>
                   </ControlTemplate>
                </Setter.Value>
             </Setter>
          </Style>
       </GroupStyle.ContainerStyle>
    </GroupStyle>
    

    However, the radio buttons will not be mutually exclusive on their own (I presume because they are wrapped in ListItem controls, or something else that makes them a single child of a grouping parent). That code was stolen/modified from the GroupStyle entry in MSDN if you want to go back for more information (their example had expanders to show/hide groups): http://msdn.microsoft.com/en-us/library/system.windows.controls.groupstyle.aspx

    Let me know if I’ve missed the point at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being new to both WPF and MVVM, I'm studying Josh Smith's article on the
I'm using MVVM in a WPF app. I'm very new to both. Let me
I’m building a WPF application using MVVM pattern (both are new technologies for me).
I'm kinda new at both programming, and WPF. I know that this is pretty
I'm working with the MVVM pattern in WPF (a bit new to both). I'd
I'm working on an WPF application using the mvvm-light framework. I'm new to both
new to WPF and MVVM, and trying to get my head around the normal
I'm just learning about MVVM in WPF, I'm completely new both to WPF as
Hi, I'm new to both Prism and WPF, and I have a question regarding
I am starting a new MVVM project with WPF, both of which I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.