Solved

Databinding a combobox on a second window to a first window ObservableCollection in WPF

Posted on 2010-08-29
2
521 Views
Last Modified: 2013-11-12
I am trying to get a wpf window (window 2) to come up with a combobox that is bound to the same observablecollection as a combobox in window 1.  so, they are 2 saparate WPF application projects in the same solution.  Window 1 has a reference to the window 2 project.

So Window 1 XAML looks like this:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,197,0,0" Name="comboBox1" VerticalAlignment="Top" Width="194" ItemsSource="{Binding Vehicles}"/>

Vehicles is a dependencyproperty that lives in the window1ViewModel class that looks like this:

       public ObservableCollection<VehicleViewModel> Vehicles
        {
            get { return (ObservableCollection<VehicleViewModel>)GetValue(VehiclesProperty); }
            set { SetValue(VehiclesProperty, value); }
        }

        public static readonly DependencyProperty VehiclesProperty =
            DependencyProperty.Register("Vehicles", typeof(ObservableCollection<VehicleViewModel>),
            typeof(Window), new UIPropertyMetadata(new ObservableCollection<VehicleViewModel>()));  

The combobox on window 1 works great (binds and reflects updates).

then I have some code that looks like this:

PopupWindow.MainWindow p = new PopupWindow.MainWindow();
p.show();

on the p xaml, I have the following:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,257,0,0" Name="comboBox1" VerticalAlignment="Top" Width="194" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}, AncestorLevel=1}, Path=Vehicles, Mode=TwoWay}"/>

note the type of window on the DependencyProperty.  I believe this is the correct way to bind it.  If I change the p xaml to something with a different AncestorLevel or Path=xxxxx, it complains that it can't find the binding when it runs, and as coded above it does not complain.  So, I think this is right.

If anyone can tell me what piece I am missing, or if this is the wrong way entirely, or anything, I would appreciate it.  It had occurred to me it may be a datacontext issue on window 2, but I didn't think it was the issue.

Simply put, I need to bind 2 comboboxes on 2 different windows to the same observablecollection.

Thanks.
0
Comment
Question by:Voltus_WoW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33556787
Try setting the Parent on the p instance.
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33649907
change the code to (assuming that the code is in the first window)

PopupWindow.MainWindow p = new PopupWindow.MainWindow();
p.DataContext = this.DataContext;
p.show();


and remove the relative source property from the binding.

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question