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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:29:20+00:00 2026-05-23T23:29:20+00:00

I have an ObservableCollection that is created in user control that contains a custom

  • 0

I have an ObservableCollection that is created in user control that contains a custom class. The custom class contains a few strings and booleans, nothing special.

public class StringLineInfo : INotifyPropertyChanged
{
    private string name { set; get; }
    private Color color { set; get; }
    private bool visible { set; get; }
    private bool follow { set; get; }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
        }
    }

    public Color Color
    {
        get { return color; }
        set
        {
            color = value;
            NotifyPropertyChanged("Color");
        }
    }

    public bool Visible
    {
        get { return visible; }
        set
        {
            visible = value;
            NotifyPropertyChanged("Visible");
        }
    }

    public bool Follow
    {
        get { return follow; }
        set
        {
            follow = value;
            NotifyPropertyChanged("Follow");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
}

Inside the Xaml of the custom control I have added custom legend control. Inside the custom legend I have another ObservableCollection that an ItemsControl is databound to.

In the legend.xaml:

                                <!--<Image Name="imgMyImage" Grid.Column="1" Height="30" Width="30" Margin="5" Source="{Binding Name, Converter=NameToSrcConverter, ConverterParameter={StaticResource IconDictionary}}" />-->
                            <TextBlock Name="txtlineName" FontSize="12" Foreground="Black"  Text="{Binding Converter={StaticResource nameConverter}}"  Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

In the legend.xaml.cs:

    public ObservableCollection<StringLineInfo> allLines = new ObservableCollection<StringLineInfo>();

    public Dictionary<string, string> DvrIconDictionary = new Dictionary<string, string>();
    public Legend()
    {
        InitializeComponent();
        DataContext = this;
        itmCtrol.ItemsSource = allLines;
    }

    // to set the ItemsSource
    public void setItemsSource(ObservableCollection<StringLineInfo> source)
    {
        if (itmCtrl.ItemsSource == null)
        {
            itmCtrl.ItemsSource = source;
        }
    }

In the constructor of the main user control I set the ObservableCollection from the legend equal to the ObservableCollection in the main user control.

public MainControl()
    {
        InitializeComponent();
        MapLegend.SizeChanged += new SizeChangedEventHandler(MapLegend_SizeChanged);
        MapLegend.allLines = this.allLines;
    }

Even though both ObservableCollections always contain the same data (they’re now the same object) the ItemsControl will not update or show anything. HOWEVER, if I set the ObservableCollections equal later in the code, say a button click, then it works just fine.

The button just calls the “setItemsSource” function.

Any ideas on why I can’t set them equal at startup?

  • 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-23T23:29:21+00:00Added an answer on May 23, 2026 at 11:29 pm

    You actually have three references to the allLines collection to which you are trying to bind.

    1. allLines in the type that contains the MainControl method.
    2. allLines within legend.xaml.cs.
    3. itmCtrol.ItemsSource withing legend.xaml.cs.

    I assume that the first reference is being populated correctly as you are eventually seeing the data. The second reference is initialized with an empty ObservableCollection that is not required (you could save yourself the overhead of initializing this object). The third is set to the same empty collection as the second.

    Your code in the MainControl method sets the second reference to the correctly populated collection but does not affect the third refernce (itmCtrl.ItemsSource) in any way – it retains its reference to the empty collection.

    My guess is that you are using code such as the following within legend.xaml.cs in the button click event that ‘works just fine’.

    itmCtrol.ItemsSource = allLines;

    This would swap the itmCtrol.ItemsSource reference from the empty collection to the correctly populated one.

    The simplest solution would be to replace the allLines field with a property that delegates straight to the itmCtrol.ItemSource property (use the itmCtrol.ItemsSource property as the backing field for the new property). It would look something like this.

        public ObservableCollection<StringLineInfo> AllLines
        {
            get
            {
                return (IObservableCollection<StringLineInfo>)itmCtrol.ItemsSource;
            }
            set
            {
                itmCtrol.ItemsSource = value;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles
I have a user control that has a grid (the one you get automatically
I have a datagrid that is bound to a observableCollection of Employees The user
I have a custom class named BlinkingLight. I also have a static ObservableCollection BlinkingLightCollection.
I have a User class and an Author class that extends User. I have
I have created an object called Project that has different properties (strings and some
I have created a user control, the other members of my team simply place
I have an : ObservableCollection<X> x_collection = new ObservableCollection(); public class X { public
I have an ObservableCollection of class Customers that I fill from a database query.
I have an ObservableCollection of addresses that I am binding to a ListBox. Then

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.