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

  • Home
  • SEARCH
  • 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 7498133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:21:13+00:00 2026-05-29T19:21:13+00:00

I am creating a Windows app using C# and WPF, using MVVM. I am

  • 0

I am creating a Windows app using C# and WPF, using MVVM. I am trying to create a collapsible items control, in order to display items from a collection. When expanding each item, a groupbox containing the item’s properties should be displayed. I have the following inside a UserControl:

<UserControl.Resources>
<SolidColorBrush x:Key="GlyphBrush" Color="#444" />
<ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
    <Grid
        Width="15"
        Height="13"
        Background="Transparent">
        <Path x:Name="ExpandPath"
          HorizontalAlignment="Left" 
          VerticalAlignment="Center" 
          Margin="1,1,1,1"
          Fill="{StaticResource GlyphBrush}"
          Data="M 4 0 L 8 4 L 4 8 Z"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked"
             Value="True">
            <Setter Property="Data"
              TargetName="ExpandPath"
              Value="M 0 4 L 8 4 L 4 8 Z"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
    <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
</Style>

<BooleanToVisibilityConverter x:Key="VisibilityOfBool" />

<Style x:Key="CollapsibleListStyle" TargetType="{x:Type ItemsControl}">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style>
                <Setter Property="Control.Margin" Value="5" />
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ContentControl">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Grid.Column="0" Focusable="False">
                                </ContentPresenter>
                                <ToggleButton x:Name="toggleButton"  
                                    Grid.Column="1" IsChecked="False" Margin="3.5" 
                                      Style="{StaticResource toggleButtonStyle}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
</UserControl.Resources>

<WrapPanel>
    <ItemsControl Name="itemsList"
                  Style="{StaticResource CollapsibleListStyle}"
                  ItemsSource="{Binding ViewModel.Items}"
                  Margin="0,0">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" Grid.Column="0" 
                      FontWeight="DemiBold" VerticalAlignment="Center"/>
                    <GroupBox Header="Properties" Grid.Column="1" Margin="5" 
                      Visibility="{Binding ElementName=toggleButton, 
                        Path=IsChecked, Converter={StaticResource VisibilityOfBool}}">
                        <WrapPanel HorizontalAlignment="Center">
                            <StackPanel Orientation="Horizontal" Margin="5">
                                <TextBlock Text="Code: "/>
                                <TextBlock Text="{Binding ItemCode}"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="5">
                                <TextBlock Text="Key: "/>
                                <TextBlock Text="{Binding ItemKey}"/>
                            </StackPanel>
                        </WrapPanel>
                    </GroupBox>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</WrapPanel>  

This produces the following error at runtime:

System.Windows.Data Error: 4 : Cannot find source for binding with
reference ‘ElementName=toggleButton’.
BindingExpression:Path=IsChecked; DataItem=null; target element
is ‘GroupBox’ (Name=”); target property is ‘Visibility’ (type
‘Visibility’)

so the toggle button is not displayed.
At a different place in my application, I have used the above, but replaced ItemsControl with a ListBox, to get a collapsible list box, and the code works as it should.
However, here I do not want the selection functionality.

Can anyone please help with this?

Thank you, Brian

  • 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-05-29T19:21:15+00:00Added an answer on May 29, 2026 at 7:21 pm

    You should always specify the TargetType if you know it, your style does not apply properly because you make it generic by not setting it and at the same time setting properties which do not exist on the container and hence are ignored.

    Change the TargetType to ContentPresenter and you will no longer be able to set a Template:

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Template"> <!-- will throw an error -->
    

    You need to move everything into the ItemTemplate as there is no ControlTemplate to be set.

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

Sidebar

Related Questions

Background: I'm creating a WPF app using MVVM, and using a DI container to
I'm creating a Windows app that automatically updates itself. I'm not using ClickOnce for
I am creating a Windows Forms control derived from UserControl to be embedded in
I'm creating interactive slideshow control in wpf/c#. I've want to create something similar to
I was trying to create an app using Bing Map. in which i need
I am creating a system tray app which monitors mouse clicks in Windows. I
I am creating windows installer project using Visual Studio 2005. Is there an option
I am creating a Windows Service in C# that processes messages from a queue.
I'm pondering creating a WPF or Silverlight app that acts just like a terminal
I am creating a windows app that uses a vector of stings as a

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.