Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:25:44+00:00 2026-06-18T07:25:44+00:00

Context: I am using EF 5 and trying to bind two entities to a

  • 0

Context: I am using EF 5 and trying to bind two entities to a WPF form. It works fine with simples property-to-textbox binding, but I’d like to bind a navigation property object to a combobox. I am learning WPF so this may be obvious.

I have two EF entities:

Product { int Id, string Name, Category Category }

and

Category { int Id, string Description }

I made a WPF form to create and edit products, it is something like this (simplified):

        <Window.Resources>
            <CollectionViewSource x:Key="productViewSource" d:DesignSource="{d:DesignInstance my:product, CreateList=True}" />
            <CollectionViewSource x:Key="categoryViewSource" d:DesignSource="{d:DesignInstance my:category, CreateList=True}" />
        </Window.Resources>
        <Grid DataContext="{StaticResource productViewSource}">
            <Label Content="Id" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="61,8,0,0" Name="txtId" Text="{Binding Path=Id, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Top" Width="56" IsEnabled="False" />
            <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="6,38,0,0" Name="label2" VerticalAlignment="Top" />
            <TextBox Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Height="23" HorizontalAlignment="Left" Margin="61,40,0,0" Name="txtNome" VerticalAlignment="Top" Width="183" IsEnabled="False" />

            <Label Content="Category" Height="28" HorizontalAlignment="Left" Margin="6,129,0,0" Name="label5" VerticalAlignment="Top" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="72,132,0,0" Name="comboCategory" VerticalAlignment="Top" Width="172" 
                      DisplayMemberPath="Description" 
                      SelectedValuePath="Id" 
                      SelectedValue="{Binding Path=Category, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      ItemsSource="{Binding Path=categoryViewSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
            </ComboBox>
         </Grid>

The code to load the two View Sources is this:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        context = new DBEntities();

        var productViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productViewSource")));
        context.Product.Include(p => p.Category);
        context.Product.Load();
        ProductViewSource.Source = context.Product.Local;

        var categoryViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource")));
        categoryViewSource.Source = context.Category.ToList();
    }

When the window loads the two textboxes work, but the combobox is always empty.

Probally this is what is wrong, but I can’t seem to find a way to make it work.

ItemsSource="{Binding Path=categoryViewSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:25:45+00:00Added an answer on June 18, 2026 at 7:25 am

    This is wrong:

    ItemsSource="{Binding Path=categoryViewSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">

    What you’re telling WPF here is that you want the Property categoryViewSource from the first Window it encounters upwards in the Visual Tree. Of course the Window Class doesn’t have such property.

    It should be:

    ItemsSource="{StaticResource categoryViewSource}">

    which tells WPF you want to look up a Resource with that name (key)

    Edit: You’d better create a ViewModel to hold your data:

    public class ViewModel: INotifyPropertyChanged
    {
         public List<Category> Categories {get;set;} //Don't forget INotifyPropertyChanged!!
    
         //... other properties
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        context = new DBEntities();
    
        var vm = new ViewModel() { Categories = context.Category.ToList(); }
    
        DataContext = vm;
    }
    

    XAML:

    <ComboBox ItemsSource="{Binding Categories}"/>
    

    Remove the Static Resources and remove all references to them.

    Edit 2:

    The SelectedValuePath And SelectedValue Properties work together. The SelectedValuePath is telling the ComboBox “Evaluate this property inside each item”, and the SelectedValue property is the actual value you expect to find in that property, so:

    <ComboBox SelectedValuePath="Id" SelectedValue="{Binding Category}"/>
    

    Is telling WPF that you have some int property called Category in your DataContext class which you expect to contain a value matching the Id of one of your entities.

    What you actually need to do here is:

    ViewModel:

    public Category Category {get;set;} //Don't forget INotifyPropertyChanged!!!
    

    XAML:

    <ComboBox SelectedItem="{Binding Category}"/>
    

    and remove both SelectedValuePath and SelectedValue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to late-bind context menus to elements, using the ContextMenu plugin .
I'm trying to do this: using(var context = new SampleEntities()) { User user =
for example I'm trying to get the data from database like: using (ExplorerDataContext context
I'm trying to use construction injection using both Jersey-injected @Context parameters and Guice-injected parameters.
I am trying to implement generic repository pattern using LINQ to SQL data context.
I am trying to run two scripts: one for loading page content using ajax
I'm using .NET 4 EF and am trying to save the child entities (InvoiceLogs)
I'm trying to make Jeditable works on new elements created using this jquery file
I am trying to bind to a property while inside a XAML 'property setter'.
i'm trying to create a context menu for jQuery, i was using the jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.