【发布时间】:2014-06-27 06:45:48
【问题描述】:
一个全新的 XAML 和 WPF 蜜蜂从 MVC 转移 1 周前
要求:
显示客户名称和所有相应的品牌
1 个客户可以拥有多个品牌
问题
无法显示对应的品牌
参考
代码
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new MyDeviceList();
InitializeComponent();
}
}
public class MyDeviceList
{
Entities ent = new Entities();
public ObservableCollection<myCustomers> Customers { get; set; }
public void GetCustomersBrand()
{
var custall = (from c in ent.Customers
select new myCustomers{ name = c.Name, brands = c.Brands.ToList() }).ToList();
Customers = new ObservableCollection<myCustomers>(custall);
}
public MyDeviceList()
{
GetCustomersBrand();
}
}
//just a dummy class
public class Brand
{
public string Name { get; set; }
public int Customerid { get; set; }
}
public class myCustomers
{
public string name { get; set; }
public List<Brand> brands { get; set; }
}
XAML
<Window x:Class="DeviceListCreate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:DeviceListCreate"
Title="MainWindow" WindowState="Maximized">
<Window.Resources>
<DataTemplate x:Key="GroupTemplate" DataType="{x:Type my:myCustomers}">
<ItemsControl ItemsSource="{Binding brands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding Customers}" Name="tStack" Grid.Column="0" Margin="33,10,42,10">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding name}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
请指导我现在应该往哪个方向前进
提前致谢
【问题讨论】:
-
你应该在你的itemscontrol中使用分组wpf-tutorial.com/listview-control/listview-grouping
-
@nit 谢谢 nit 我想我需要这样的东西来调查它
-
编辑了我的问题,但实际上我的问题是如何在我的
<ItemsControl.ItemTemplate>@strattonn 中绑定/调用 DataTemplate 'GroupTemplate' -
@ArijitMukherjee 一个更简单的解决方案是拥有一个 ItemsControl,其中 Customer 的每个项目模板都是一个面板,其中名称为 TextBlock,另一个 ItemControl 与 Brands Bound 作为 ItemsSource
-
@eranotzap 好吧,这正是我正在尝试和想要做的,但无法绑定品牌 :( 所以我尝试在数据模板中编写另一个 ItemControl,你可以在资源中看到它,这给了品牌但我并没有将品牌与客户完全绑定
标签: c# wpf xaml data-binding wpf-controls