Wpf Combobox Binding in ListView

I have a problem with data binding in a drop down list. I have two classes:

  • Deal
  • substrate

A transaction has a Substrate attribute, and Transactions are stored in the Database. At the beginning of the program, I want to load all the transactions in a list and show them in a ListView. Each substrate capability should be shown in a combo box where the actual substrate is selected.

I tried like this XAML

<ListView.View> <GridView> <GridViewColumn Header="Menge"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Path=Amount}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Substrate"> <GridViewColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding ElementName=InternTransaction, Path=SubstrateList}" DisplayMemberPath="Description" SelectedValuePath="SubstrateID" SelectedItem="{Binding Path=Substrate.SubstrateID}"> </ComboBox> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> 

Code for

 public partial class UCInternTransaction : UserControl { #region Attribute private BsCBTTransactionController mTransactionController; private ObservableCollection<BsCBTSubstrate> mSubstrateList; #endregion public UCInternTransaction() { InitializeComponent(); //Load Transactions this.mTransactionController = WpfBioGas.Core.BsCAppFactory.getInstance().getCBTTransactionController(); this.mTransactionController.loadTransactions(); this.DataContext = this.mTransactionController.TransactionList; loadData(); } private void loadData() { //Load Substrate and bind to CBSubstrate this.mSubstrateList = new ObservableCollection<BsCBTSubstrate>(); foreach (BsCBTSubstrate sub in WpfBioGas.Core.BsCAppFactory.getInstance().getBTFacade().BsBTSubstrate.loadAll()) { this.mSubstrateList.Add(sub); } } public ObservableCollection<BsCBTSubstrate> SubstrateList { get { return this.mSubstrateList; } } } 

The problem is that all the entries in the list are displayed in the form of a list, and for each row all the possibilities of the substrate are in the combined list. But only for the first row of the ListView is the actual substrate selected.

+4
source share
1 answer

Your ComboBox should use the binding to SelectedValue, not SelectedItem.

It’s a little difficult to provide a fix based only on the fragments that you indicated in your post, but here kaxaml is already an example that uses several built-in XML data sources:

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <XmlDataProvider x:Key="CharacterData"> <x:XData> <Data xmlns=""> <Character First="Bart" Last="Simpson" Gender="M"/> <Character First="Homer" Last="Simpson" Gender="M"/> <Character First="Lisa" Last="Simpson" Gender="F"/> <Character First="Maggie" Last="Simpson" Gender="F"/> <Character First="Marge" Last="Simpson" Gender="F"/> </Data> </x:XData> </XmlDataProvider> <XmlDataProvider x:Key="GenderData"> <x:XData> <Data xmlns=""> <Gender ID="F" Description="Female" /> <Gender ID="M" Description="Male" /> </Data> </x:XData> </XmlDataProvider> </Page.Resources> <ListView ItemsSource="{Binding Source={StaticResource CharacterData}, XPath=Data/Character}"> <ListView.View> <GridView> <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding XPath=@First }" /> <GridViewColumn Header="Gender"> <GridViewColumn.CellTemplate> <DataTemplate> <ComboBox Width="75" SelectedValue="{Binding XPath=@Gender }" DisplayMemberPath="@Description" SelectedValuePath="@ID" ItemsSource="{Binding Source={StaticResource GenderData}, XPath=Data/Gender}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> </Page> 
+2
source

Source: https://habr.com/ru/post/1300628/


All Articles