I have the following code in Wpf form:
public class Product
{
public string Name { get; set; }
public string Type { get; set; }
}
public class ProductWithValue
{
public Product Object_Product { get; set; }
public string Value { get; set; }
}
public class Data
{
public ObservableCollection<Product> ListProduct { get; set; }
public ObservableCollection<ProductWithValue> ListProdValue { get; set; }
}
Data data = LoadData();
lstProducts.DataContext = data;
The XAML:
<ListView Name="lstProducts" ItemsSource="{Binding Path=ListProdValue}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="23">
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ListProduct}"
SelectedItem="{Binding Path=Object_Product, Mode=TwoWay}"
DisplayMemberPath="Name"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
LoadData example:
private Data LoadData()
{
Data data = new Data();
Product prod1 = new Product(){Name="Name1", Type = "Type1"};
Product prod2 = new Product(){Name="Name2", Type = "Type2"};
data.ListProduct = new ObservableCollection<Product>()
{
prod1,
prod2
};
data.ListProdValue = new ObservableCollection<ProductWithValue>()
{
new ProductWithValue(){ Object_Product = prod1, Value="Value" }
};
return data;
}
Evrything is fine, except the DisplayMemberPath. The combobox itemssource is correct, the selected item is correct, but does not show the Name of the Product.
Any idea whats going wrong?
LoadData
method? – kmatyaszek