Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DB Mail Export to Excel some row wrap to new row 30 109
Messagebox for a Web Site Application 4 63
Poor Man's .NET Framework Certification 2 47
windbg taking it's time 32 80
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

20 Experts available now in Live!

Get 1:1 Help Now