Wpf listview 组合框绑定 DisplayMemberPath 不起作用
Wpf listview combobox binding DisplayMemberPath not working
我有以下 Wpf 形式的代码:
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;
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>
加载数据示例:
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;
}
一切都很好,除了 DisplayMemberPath。
combobox itemssource是正确的,选择的项目是正确的,但没有显示产品名称。
知道出了什么问题吗?
我的代码中错误的 LoadData 函数:
private Data LoadData()
{
Data data = new Data();
Product prod1 = new Product() { Name = "Name1", Type = "Type1" };
Product prod2 = new Product() { Name = "Name2", Type = "Type2" };
Product prod3 = new Product() { Name = "Name3", Type = "Type3" };
data.ListProduct = new ObservableCollection<Product>()
{
prod1,
prod2,
prod3
};
data.ListProdValue = new ObservableCollection<ProductWithValue>()
{
new ProductWithValue(){ Object_Product = new Product() { Name = "Name1", Type = "Type1" }, Value="Value1" },
new ProductWithValue(){ Object_Product = new Product() { Name = "Name2", Type = "Type2" }, Value="Value2" }
};
return data;
}
我有以下 Wpf 形式的代码:
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;
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>
加载数据示例:
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;
}
一切都很好,除了 DisplayMemberPath。 combobox itemssource是正确的,选择的项目是正确的,但没有显示产品名称。
知道出了什么问题吗?
我的代码中错误的 LoadData 函数:
private Data LoadData()
{
Data data = new Data();
Product prod1 = new Product() { Name = "Name1", Type = "Type1" };
Product prod2 = new Product() { Name = "Name2", Type = "Type2" };
Product prod3 = new Product() { Name = "Name3", Type = "Type3" };
data.ListProduct = new ObservableCollection<Product>()
{
prod1,
prod2,
prod3
};
data.ListProdValue = new ObservableCollection<ProductWithValue>()
{
new ProductWithValue(){ Object_Product = new Product() { Name = "Name1", Type = "Type1" }, Value="Value1" },
new ProductWithValue(){ Object_Product = new Product() { Name = "Name2", Type = "Type2" }, Value="Value2" }
};
return data;
}