-1

I am trying to learn WPF and binding but don't seem to be able to understand what is going on. I have copied a code sample using a combobox and have been trying modifying it to see what happens. The problems appears to be with setting the datacontext. If I set it after InitializeComponent(); with DataContext = this; everything works great. However, I can't get the sample to work with any other DataContext setting. I have tried

DataContext="{Binding RelativeSource={RelativeSource Self}}"

Name="_this" DataContext="{Binding ElementName=_this}"

with no data in my combobox.

The code behind is:

cbItems = new ObservableCollection<ComboBoxItem>();
            var cbItem = new ComboBoxItem { Content = "All" };
            SelectedItem = cbItem;
            cbItems.Add(cbItem);
            var items = (from a in dc.Counties
                select a.CountyName).ToList();

            foreach (var item in items)
            {
                //cbItems.Add(cbItem);
                cbItems.Add(new ComboBoxItem{Content = item});
            }

The xaml is

<ComboBox x:Name="ComBoCounty" HorizontalAlignment="Left" Margin="44,38,0,0" VerticalAlignment="Top"
                  Width="120" ItemsSource="{Binding cbItems}" SelectedItem="{Binding SelectedcbItem}"
                  Loaded="ComBoCounty_OnLoaded" SelectionChanged="ComBoCounty_OnSelectionChanged" Height="20" />

I use ReSharper and it gives me an indication of an error and has recommendations for DataContext values. I have tried them all with with no data in my combobox. This seems like it is one of the most basic things to understand and I have spent lots of time trying to get it. If someone could show me a way that this could work without setting the DataContext in xaml and why it works I would appreciate it.

  • What kind of DataContext do you want to set? Another class? The DC applies throughout the Xaml unless you're explicitly changing it. Can you check the output window in VS for binding errors (on running the app)? – Lennart Jul 08 '15 at 06:46
  • The perfect answer to this question is here: http://stackoverflow.com/questions/25549826/resharper-wpf-error-cannot-resolve-symbol-myvariable-due-to-unknown-datacont – Contango Jul 08 '15 at 06:53

1 Answers1

0

ItemsSourceYou can bind the DataContext to your window
(which would be the same as you tried in code by setting DataContext = this):

    <Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
       <ComboBox ItemsSource="{Binding Path=cbItems}" ...></ComboBox>
    </Grid>
Denis Thomas
  • 1,012
  • 1
  • 8
  • 17
  • Thanks this worked! So RelativeSource is working up the tree to find Window. If this were part of a multi-window app would it just find the current active window and stop there? I think the tree is for the entire app, not just the current window. Your help is really appreciated. – user2681786 Jul 08 '15 at 07:35
  • It will use the window the control is located in, using the first match. By the way this is just one possible solution, as an alternative you can also use a DataContext defined as a resource for example. – Denis Thomas Jul 08 '15 at 07:38
  • I tried moving to a much larger app with multiple ComboBoxs, DataGrids, etc. and the solution above did not work. In the output window I am getting the error:System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') – user2681786 Jul 08 '15 at 14:13
  • This is not enough information to analyse, can you provide the XAML? – Denis Thomas Jul 09 '15 at 06:10