【问题标题】:Grid View or ListView Binding or Rendering of Collection of CollectionGrid View 或 ListView 的 Collection 的绑定或呈现
【发布时间】:2014-06-27 06:45:48
【问题描述】:

一个全新的 XAML 和 WPF 蜜蜂从 MVC 转移 1 周前

要求:

显示客户名称和所有相应的品牌

1 个客户可以拥有多个品牌

问题

无法显示对应的品牌

参考

Similar SO Thread

代码

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 我想我需要这样的东西来调查它
  • 编辑了我的问题,但实际上我的问题是如何在我的&lt;ItemsControl.ItemTemplate&gt;@strattonn 中绑定/调用 DataTemplate 'GroupTemplate'
  • @ArijitMukherjee 一个更简单的解决方案是拥有一个 ItemsControl,其中 Customer 的每个项目模板都是一个面板,其中名称为 TextBlock,另一个 ItemControl 与 Brands Bound 作为 ItemsSource
  • @eranotzap 好吧,这正是我正在尝试和想要做的,但无法绑定品牌 :( 所以我尝试在数据模板中编写另一个 ItemControl,你可以在资源中看到它,这给了品牌但我并没有将品牌与客户完全绑定

标签: c# wpf xaml data-binding wpf-controls


【解决方案1】:

在构建这样的 UI 时,您只需逐个构建它。单独专注于每个部分使整个任务更易于管理。试试这个:

<Grid>
    <ItemsControl ItemsSource="{Binding Customers}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="5" BorderBrush="RoyalBlue" BorderThickness="1" 
                    Padding="5" Margin="5">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding name}" Margin="5" />
                        <ItemsControl ItemsSource="{Binding brands}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox Content="{Binding name}" Margin="5" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

【讨论】:

  • 非常感谢您的回复,但仍然没有显示品牌名称:(
  • 感谢它的工作我无法将品牌绑定到客户而不是 &lt;ItemsControl ItemsSource="{Binding brands}"&gt; 我正在做 &lt;DataTemplate DataType="{x:Type my:myCustomers}"&gt; &lt;ItemsControl ItemsSource="{Binding brands}"&gt; 因为一旦我进入我的课程我就不需要参考我的课程 谢谢又很多:)
  • 我现在已经更新了您的 brands 属性的名称...但请注意,如果您没有在属性名称上使用正确和预期的大小写,就会发生这种情况。跨度>
  • 实际上我只是为 dummy 创建了 Brand 类 :) 并犯了错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 2011-04-09
相关资源
最近更新 更多