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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:31:12+00:00 2026-05-19T14:31:12+00:00

I’m trying to build a UserControl which includes 2 listBoxes. I then wan’t to

  • 0

I’m trying to build a UserControl which includes 2 listBoxes. I then wan’t to set the itemsSources for the listboxes where I use the UserControl.

As I understand that is what DependencyProperties are for. However I’m not successful in doing this. I do believe it mostly has to do with the timing of initialization. Any pointer on what I’m doing right, how I can make it better and such is welcome.

Here is my user control, I’m learning as I’m going so I guess I could do it better

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>

    <ListBox Name="SET" ItemsSource="{Binding Path=SetCollection}" />

    <UniformGrid Rows="4" Grid.Column="1">

        <Grid />

        <Button Margin="10" Click="selectionBtnClick">--></Button>

        <Button Margin="10" Click="removeBtnClick">Remove</Button>

    </UniformGrid>

    <ListBox Name="SUBSET" ItemsSource="{Binding Path=SubsetCollection}" Grid.Column="2" />

</Grid>

CodeBehind

public partial class SubsetSelectionLists : UserControl
{
    public static DependencyProperty SetCollectionProperty = DependencyProperty.Register("SetCollection", typeof(CollectionView), typeof(SubsetSelectionLists));
    public static DependencyProperty SubsetCollectionProperty = DependencyProperty.Register("SubsetCollection", typeof(CollectionView), typeof(SubsetSelectionLists));

    public CollectionView SetCollection
    {
        get
        {
            return (CollectionView) GetValue(SetCollectionProperty);
        }
        set
        {
            SetValue(SetCollectionProperty, value);
        }
    }

    public CollectionView SubsetCollection
    {
        get
        {
            return (CollectionView)GetValue(SubsetCollectionProperty);
        }
        set
        {
            SetValue(SubsetCollectionProperty, value);
        }
    }

    public SubsetSelectionLists()
    {
        InitializeComponent();
    }

    private void selectionBtnClick(object sender, RoutedEventArgs e)
    {
        SUBSET.Items.Add(SET.SelectedItem);
    }

    private void removeBtnClick(object sender, RoutedEventArgs e)
    {
        SUBSET.Items.Remove(SUBSET.SelectedItem);
    }
}

And the code behind where I use it

public partial class SomeWindow : Window
{
    ViewModel vm = new ViewModel();

    public SomeWindow()
    {
        InitializeComponent();
        NameOfUserControl.SetCollection = vm.InputView;
        NameOfUserControl.SubsetCollection = vm.OutputView;
    }
}

Here the SetCollection is set as inputView and then later tied to the DependencyProperty severing the original binding I think. Any idea where I’m going wrong?

Ps. subquestion – As I will be moving from one collection to the other shouldn’t I ensure somehow that the collection hold objects off the same type? How could I do that?

  • 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-19T14:31:13+00:00Added an answer on May 19, 2026 at 2:31 pm

    First of all you should set the DataContext property in SomeWindow to vm. This will allow very simple binding expression in your SomeWindow.xaml.

    public partial class SomeWindow : Window
    {
        ViewModel vm = new ViewModel();
    
        public SomeWindow()
        {
            InitializeComponent();
            this.DataContext = vm;
        }
    }
    

    In SomeWindow.xaml:

    <local:SubsetSelectionLists
        SetCollection="{Binding Path=InputView}"
        SubsetCollection ="{Binding Path=OutputView}" />
    

    There are several ways to solve the binding problem in the user control:

    Setting the data context

    Add the following to the constructor of the user control.

    this.DataContext = this;
    

    Wpf binds against the current data context, unless a specific source (e.g. ElementName) has been set.

    Use binding with ElementName

    You could reference the user control in the binding expressions.

    <UserControl x:Class="WpfApplication1.SubsetSelectionLists"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
                 x:Name="root">
    <Grid>
    ...
        <ListBox Name="SET"
                 ItemsSource="{Binding Path=SetCollection, ElementName=root}" />
    ...
        <ListBox Name="SUBSET" Grid.Column="2"
                 ItemsSource="{Binding Path=SubsetCollection, ElementName=root}" />
    
    </Grid>
    </UserControl>
    

    Bind to the VM

    Another method would be to set the data context of the user control in SomeWindow and bind to the Properties of the VM. You could then remove the dependency properties of the user control.

    public partial class SomeWindow : Window
    {
        ViewModel vm = new ViewModel();
        //with properties InputView and OutputView
    
        public SomeWindow()
        {
            InitializeComponent();
            NameOfUserControl.DataContext = vm;
        }
    }
    

    In the user control:

    <Grid>
    ...
        <ListBox Name="SET"
                 ItemsSource="{Binding Path=InputView}" />
    ...
        <ListBox Name="SUBSET" Grid.Column="2"
                 ItemsSource="{Binding Path=OutputView}" />
    
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.