我在listview中对组合框的数据绑定有问题.
我有两个班:
– 交易
– 基质
Transaction具有Substrate的属性,Transactiona保存在Datebase中.在程序开始时,我想将所有事务作为列表加载并在ListView中显示它们. Substrate的每种可能性都应该在Combobox中显示,其中选择了实际的Substrate.
我试过这样的
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>
代码隐藏
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; }
}
}
问题是列表中的所有条目都显示在列表视图中,对于每一行,Substrate的所有可能性都在Combobox中.但只有Listview的第一行才会选择实际的Substrate.
最佳答案 您的ComboBox应该使用SelectedValue而不是SelectedItem的绑定.
基于您在帖子中显示的片段提供修复有点困难,但这里有一个使用了几个内联XML数据源的kaxaml样本:
<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>