?
Solved

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

Posted on 2010-08-29
2
Medium Priority
?
524 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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