Reputation: 55
I've got a problem with the databinding of a combobox in a listview. I have two classes:
the Transaction has an attribute of the Substrate and the Transactiona are saved in a Datebase. At the start of the program I want to load all Transactions as a list and show them in a ListView. Each possibility of Substrate should be shown in a Combobox, where the actual Substrate is selected.
I've tried it 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-behind
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 entries of the list are shown in the listview and for each row all possibilities of the Substrate are in the Combobox. But only for the first row of the Listview the actual Substrate is selected.
Upvotes: 4
Views: 9563
Reputation: 646
Your ComboBox should use a binding on SelectedValue rather than SelectedItem.
It's a bit difficult to provide a fix based on just the snippets you've shown in your post, but here is a kaxaml-ready sample that uses a couple of inline 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>
Upvotes: 2