?
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

839 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