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 6049889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:37:38+00:00 2026-05-23T07:37:38+00:00

I have a UserControl that contains a ListBox that is bound to a CollectionViewSource

  • 0

I have a UserControl that contains a ListBox that is bound to a CollectionViewSource called Data and each item in that collection is displayed in the ListBox using an ItemTemplate. That ItemTemplate is an ItemsControl that is bound to another CollectionViewSource called Rows. Rows stores one or more MyListBoxRow objects. For each object in the Data CollectionViewSource, I get a ListBoxItem which is comprised of ContentPresenters from the Rows CollectionViewSource.

The reason I’m doing this is so I can manipulate the Rows collection at run-time and add/remove “rows” of information.

The problem I’m encountering is with the data binding inside the “NumberDescriptionDataTemplate”, “NotesDataTemplate” and “AuditDataTemplate” DataTemplates. For example, {Binding Notes} inside the NotesDataTemplate doesn’t work because the current item being bound is from Rows, not Data. If I change the NotesDataTemplate to {Binding Description} I get the Description from the MyListBoxRow object as expected.

How do I modify the Binding statement in my DataTemplates so the information is bound to an item from the Data collection instead of an item from the Rows collection?

MyListBox.xaml…

<UserControl x:Name="MyListBox"...>
<UserControl.Resources>
    <CollectionViewSource x:Key="Data" Source="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
    <CollectionViewSource x:Key="Rows" Source="{Binding ListRows, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Filter="Rows_Filter" />
</UserControl.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource Data}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Source={StaticResource Rows}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ContentPresenter ContentTemplate="{Binding RowTemplate}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
</UserControl>

“MyListBox” in use…

<Window...>
<Window.Resources>
    <DataTemplate x:Key="NumberDescriptionDataTemplate">
        <TextBlock Text="{Binding Number_Description}"  FontSize="20" />
    </DataTemplate>
    <DataTemplate x:Key="NotesDataTemplate">
        <TextBlock Text="{Binding Description}"  />
    </DataTemplate>
    <DataTemplate x:Key="AuditDataTemplate">
        <TextBlock FontSize="8pt" FontStyle="Italic" TextTrimming="CharacterEllipsis">
            <TextBlock.Text>
                <MultiBinding StringFormat="{}Added On {0:ddd MMM dd, yyyy hh:mm:ss}; Last Modified On {1:ddd MMM dd, yyyy hh:mm:ss}; Removed On {2:ddd MMM dd, yyyy hh:mm:ss}">
                    <Binding Path="AddedOn" FallbackValue="[Added On]" TargetNullValue="n/a" />
                    <Binding Path="ModifiedOn" FallbackValue="[Modified On]" TargetNullValue="n/a" />
                    <Binding Path="RemovedOn" FallbackValue="[Removed On]" TargetNullValue="n/a" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</Window.Resources>

<local:MyListBox ItemsSource="{Binding Samples}">
    <local:MyListBox.ListRows>
        <local:MyListBoxRow Description="Number, Description"  
                            IsDisplayed="True"  
                            IsRequired="True" 
                            RowTemplate="{StaticResource NumberDescriptionDataTemplate}" />
        <local:MyListBoxRow Description="Notes" 
                            IsDisplayed="True" 
                            IsRequired="False" 
                            RowTemplate="{StaticResource NotesDataTemplate}" />
        <local:MyListBoxRow Description="Added, Modified, Removed" 
                            IsDisplayed="True" 
                            IsRequired="False" 
                            RowTemplate="{StaticResource AuditDataTemplate}" />
    </local:MyListBox.ListRows>
</local:MyListBox>
</Window>
  • 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-23T07:37:39+00:00Added an answer on May 23, 2026 at 7:37 am

    You can use other sources than the DataContext if that is not what you need at that point. When working with ItemsControls you often may want to access the DataContext of said control rather than the item that is being templated, for that you can use a RelativeSource-Binding, e.g.

    {Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.Data}
    

    That is an example, not quite sure i got the whole of your construction, you might need to adapt it.

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

Sidebar

Related Questions

I have a Usercontrol that contains a ListBox (lstClients) and a ComboBox The ListBox
I have created a UserControl that contains an Ellipse and a few lines. I
I have a WPF app with a usercontrol that contains a HwndHost. The HwndHost
I have a web user control that contains several other (web user) controls and
I have a custom user control that contains asp:ValidationSummary . It is placed on
I have a page that contains a user control that is just a personalized
I have an ASP.NET user control that contains a text box control and a
I have an ASP.Net web user control that contains a TextBox and a calendar
I have a user control that is pretty basic. It contains several TextBox controls,
I have a usercontrol that has several public properties. These properties automatically show up

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.