Solved

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

Posted on 2010-08-29
2
512 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now