question

CharlesHe-MSFT avatar image
0 Votes"
CharlesHe-MSFT asked ·

populate cascading combobox with Entity Framework

This is a MSDN question asked by zleug, the source is populate cascading combobox with Entity Framework.



Hi All.

I created simple WPF form with two ComboBoxes. I populated one ComboBox

 public List<Customer> GetApplication()
         {
             using (CustModel customer = new CustModel())
             {
                 List<Customer> cust = customer.Customers.Distinct().ToList();
    
                 cust.Insert(0, new Customer
                 {
                     CustomerID = "0",
                     ContactName = "Please select"
                 });
    
                 //Assign Entity as DataSource.
                 cbCustomer.ItemsSource = cust;
                 cbCustomer.DisplayMemberPath = "ContactName";
                 cbCustomer.SelectedValue = "CustomerID";
                 cbCustomer.Text = "Please select";
    
                 return cust;
             }
         }

How to select in the List only ContactName column and distinct it?

In SQL look like: SELECT distinct [ContactName] FROM Customer

And how populate second ComboBox depended from section from first Combobox?

Thanks.

windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!

The reply form Peter Fleischer



Xaml:

 <Window x:Class="WpfApp1.Window10"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:WpfApp10"
         mc:Ignorable="d"
         Title="Window10" Height="450" Width="800">
   <Window.DataContext>
     <local:ViewModel/>
   </Window.DataContext>
   <Grid>
     <DataGrid ItemsSource="{Binding View}"/>
   </Grid>
 </Window>

ViewModel:

 using System.ComponentModel;
 using System.Linq;
 using System.Windows;
 using System.Windows.Data;
    
 namespace WpfApp10
 {
   public partial class ViewModel
   {
     public ViewModel()
     {
       cvs.Source= from item in (new WpfApp1.DemoDBEntities()).Tab1.AsEnumerable() select item;
     }
     CollectionViewSource cvs = new CollectionViewSource();
     public ICollectionView View { get => cvs.View; }
   }
 }

In demo I use catalog "Demo" with table "Tab1".

Thanks.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.