0
            [![Grid Example][1]][1]Image refers to data that will be populated dynamically from database.will change based on button clicked from side menu.
            All blue data will be in list of containing object inside it will list of all  items 


              [1]: https://i.stack.imgur.com/OV10Q.png


            This is my model :

             public class RestaurantMenu
                {
                    public int MenuId { get; set; }
                    public string MenuName { get; set; }
                    public IList<RestaurantSubMenu> SubMenus { get; set; }
                }

                public class RestaurantSubMenu
                {
                    public int SubMenuId { get; set; }
                    public string SubMenuName { get; set; }
                    public int MenuId { get; set; }
                    public bool HasToppings { get; set; }
                    public bool HasSizes { get; set; }
                    public bool HasModels{ get; set; }
                    public IList<RestaurantProduct> Products { get; set; }
                }

                public class RestaurantProduct
                {
                    public long ProductId { get; set; }
                    public string ProductName { get; set; }
                    public string ProductDescription { get; set; }
                    public long MenuId { get; set; }
                    public long SubMenuId { get; set; }
                    public int IsNonVeg { get; set; }
                    public double ProductPrice { get; set; }
                    public string ProductPhoto { get; set; }
                    public int MaximumToppingCount { get; set; }
                    public IList<RestaurantProductSize> ProductSizes { get; set; }
                }

我如何将它绑定到统一面板?我想通过记录和绑定从 RestaurantProduct 绑定 ProductName,然后显示类似按钮。我如何将它绑定到统一面板?我想通过记录和绑定从 RestaurantProduct 绑定 ProductName,然后显示类似按钮。我如何将它绑定到统一面板?我想通过记录和绑定从 RestaurantProduct 绑定 ProductName,然后显示类似按钮。我如何将它绑定到统一面板?我想通过记录和绑定从 RestaurantProduct 绑定 ProductName,然后显示类似按钮。

WPF XAML 代码,用于绑定产品名称,但无法正常工作。需要帮助我们如何将其添加到视图中,就像共享图像 WPF XAML 代码一样,用于绑定产品名称但无法正常工作。需要帮助如何才能我们将其添加到我们的视图中,就像共享图像 WPF XAML 代码用于绑定产品名称但它不起作用。需要帮助我们如何将其添加到我们的视图中,就像共享图像 WPF XAML 代码用于绑定产品名称一样但它不起作用。需要帮助我们如何将其添加到我们的视图中,就像共享图像 WPF XAML 代码用于绑定产品名称但它不起作用。需要帮助我们如何将它添加到我们的视图中,就像共享图像一样 WPF XAML 代码,用于绑定产品名称,但无法正常工作。需要帮助我们如何将其添加到视图中,就像共享图像 WPF XAML 代码一样,用于绑定产品名称但无法正常工作。需要帮助如何才能我们将其添加到我们的视图中,就像共享图像一样

    <ItemsControl Grid.Column="1"  ItemsSource="{Binding Path=RestaurantMenu}" >
                          <ItemsControl.ItemTemplate>
                             <DataTemplate>
                                <ItemsControl ItemsSource="{Binding Path=SubMenuName}">
                                   <ItemsControl.ItemTemplate>
                                      <DataTemplate>
                                         <ItemsControl ItemsSource="{Binding Path=Products}">
                                            <ItemsPanelTemplate>
                                               <UniformGrid Columns="5" />
                                            </ItemsPanelTemplate>
                                            <ItemsControl.ItemTemplate>
                                               <DataTemplate>
                                                  <Button Content="{Binding  Path= ProductName}"/>
                                               </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                         </ItemsControl>
                                      </DataTemplate>
                                   </ItemsControl.ItemTemplate>
                                </ItemsControl>
                             </DataTemplate>
                          </ItemsControl.ItemTemplate>
                       </ItemsControl>
4

1 回答 1

0

ItemsControl你可以很容易地使用一个inside another ItemsControl,使用UniformGridfor 布局来做这样的事情:

在此处输入图像描述

XAML:

<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowVm />
    </Window.DataContext>
    <ScrollViewer>
        <ItemsControl Margin="8" ItemsSource="{Binding Path=Catagories}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding Path=Products}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="5" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock
                                    Margin="1"
                                    Padding="1"
                                    FontSize="24"
                                    FontWeight="Bold"
                                    Foreground="White"
                                    Text="{Binding Path=Name}"
                                    TextAlignment="Center"
                                    TextWrapping="Wrap">
                                    <TextBlock.Background>
                                        <SolidColorBrush Color="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.Color}" />
                                    </TextBlock.Background>
                                </TextBlock>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Window>

视图模型:

class MainWindowVm
{
    public MainWindowVm()
    {
        Catagories = new ObservableCollection<Catagory>(new[]
                                                            {
            new Catagory(new[]
                             {
                                 new Product() { Name = "Chinese" },
                                 new Product() { Name = "Free Choice" },
                                 new Product() { Name = "BBQ-Original" },
                                 new Product() { Name = "Beefy One" },
                                 new Product() { Name = "GoGo Special" },
                                 new Product() { Name = "Ham & Mushroom" },
                                 new Product() { Name = "Hawaiian" },
                                 new Product() { Name = "Meaty-One" },
                                 new Product() { Name = "Pepperoni-Plus" },
                                 new Product() { Name = "GoGo Special" },
                                 new Product() { Name = "Alligator" },
                                 new Product() { Name = "Half & Haf" },
                                 new Product() { Name = "New Yorker" },
                                 new Product() { Name = "classic" },
                                 new Product() { Name = "Garlic Sizzeler" },
                                 new Product() { Name = "Double Pepperoni" },
                                 new Product() { Name = "Chicken Delux" },
                             },
                         Colors.Blue),
            new Catagory(new[]
                             {
                                 new Product() { Name = "Chicken Hot" },
                                 new Product() { Name = "Mexicana" },
                                 new Product() { Name = "American Hot" },
                                 new Product() { Name = "Flaming Touch" },
                                 new Product() { Name = "Maxican Passion" },
                                 new Product() { Name = "BBQ Beast" },
                             },
                         Colors.Red),
            new Catagory(new[]
                             {
                                 new Product() { Name = "Vegitarian Hot" },
                                 new Product() { Name = "Vegitarian" },
                                 new Product() { Name = "Cheese & Tomato" },
                             },
                         Colors.Green),
            new Catagory(new[]
                             {
                                 new Product() { Name = "7\" Pizza 2 Toppings" },
                                 new Product() { Name = "Lunchpox pizza" },
                             },
                         Colors.Orange),
            });
    }

    public ObservableCollection<Catagory> Catagories { get; }
}

class Catagory
{
    public Catagory(IEnumerable<Product> products, Color color)
    {
        Products = new ObservableCollection<Product>(products);
        Color = color;
    }

    public ObservableCollection<Product> Products { get; }
    public Color Color { get; }
}

class Product
{
    public string Name { get; set; }
}

当然,您将希望实现您的真实视图模型INotifyPropertyChanged

于 2018-03-19T13:41:56.357 回答