Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.