【发布时间】:2019-04-10 09:59:23
【问题描述】:
我正在尝试在 ItemsControl 布局中显示一组项目,该布局放置在另一个 userControl 中,但它不会在这里显示任何内容,这是我尝试知道大部分代码是从正在工作的工作示例中复制的,我只是不能找出在 wpf 中没有经验的问题:
MainWindow.xaml
<Window x:Class="cafeteria.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:cafeteria"
xmlns:local1="clr-namespace:cafeteria._shared"
xmlns:_pages="clr-namespace:cafeteria._pages"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
Title="منظومة الكافتريا "
WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height="19*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Grid.RowSpan="2" Background="#be2116" Orientation="Vertical">
<!-- add the control buttons-->
<local1:ApplicationControls></local1:ApplicationControls>
<!--app name-->
<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Image Source="./_Content/logo.png" Grid.ColumnSpan="2" Height="80"></Image>
<TextBlock FontSize="20" Grid.Column="1" Grid.Row="1" FontWeight="Bold" TextAlignment="Left" Foreground="White">منظومة</TextBlock>
<TextBlock FontSize="20" Margin="0,0,10,0" Grid.Column="0" Grid.Row="1" FontWeight="Light" TextAlignment="right" Foreground="White">الكافتريا</TextBlock>
</Grid>
<local1:SideBar Margin="0,50,0,0"></local1:SideBar>
</StackPanel>
<StackPanel Background="#fe8d00" Grid.Row="0" Grid.Column="0"></StackPanel>
<_pages:Food Grid.Column="0" Grid.Row="1"></_pages:Food>
</Grid>
</Window>
食品用户控制 xaml 文件
<UserControl x:Class="cafeteria._pages.Food"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:cafeteria._pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<ItemsControl ItemsSource="{Binding products}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="Black" Text="{Binding Path=name}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
Food.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace cafeteria._pages
{
/// <summary>
/// Interaction logic for Food.xaml
/// </summary>
public partial class Food : UserControl
{
public List<product> products;
public Food()
{
this.products = new List<product>()
{
new product{ name = "chips"},
new product{ name = "pipsi"},
};
InitializeComponent();
}
}
public class product
{
public string name { get; set; }
}
}
【问题讨论】:
-
ItemsSource="{Binding products}"在 UserControl 的 XAML 中需要一个公共属性products在 UserControl 的 DataContext 中。此源集合通常不会由 UserControl 本身声明,而是通过继承的 DataContext 传递,例如MainWindow 的 DataContext 包含 MainWindow 的视图模型。您的控件应该如何工作?在 MainWindow XAML 中声明产品集合时是否应将其传递给 UserControl? -
如果 UserControl 只包含一个 ItemsControl,那你为什么要拥有它呢?为什么不直接使用 ItemsControl 而不是 UserControl?
-
@Clemens 我有 4 个用户控件,它们充当该项目的页面。我想要做的是,当我从导航栏导航到这些用户控件之一时,它会在初始化时从数据库中加载数据
-
然后,UserControl 可以在其初始化代码中设置它,而不是绑定它的 ItemsControl 的 ItemsSource。分配一个名称,如
<ItemsControl x:Name="itemsControl">,并在 InitializeComponent 被调用后设置 ItemsSource 属性(例如在 Loaded 事件处理程序中),如itemsControl.ItemsSource = products; -
@Clemens 非常感谢它的工作,请您将其添加为答案,以便我批准它