【问题标题】:ItemsControl layout won't display a collectionItemsControl 布局不会显示集合
【发布时间】: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。分配一个名称,如 &lt;ItemsControl x:Name="itemsControl"&gt;,并在 InitializeComponent 被调用后设置 ItemsSource 属性(例如在 Loaded 事件处理程序中),如 itemsControl.ItemsSource = products;
  • @Clemens 非常感谢它的工作,请您将其添加为答案,以便我批准它

标签: c# wpf xaml


【解决方案1】:

由于要在 UserControl 的 ItemsControl 中显示的数据不应该从继承的 DataContext(例如 Window 的视图模型)提供,而是由控件的代码逻辑在内部检索,因此您可以直接在代码中设置 ItemsSource 属性后面。

最简单的方法是将 x:Name 分配给 ItemsControl

<ItemsControl x:Name="itemsControl">

然后在调用 InitializeComponent 之后,在后面的代码中访问生成的字段:

itemsControl.ItemsSource = new List<product>()
{
    new product{ name = "chips" },
    new product{ name = "pipsi" },
};

请注意,在任何情况下都不应将 UserControl 的 DataContext 设置为自身。如果您稍后将可绑定属性添加到控件,以这种方式设置 DataContext 会破坏其属性的任何绑定到继承的 DataContext 中视图模型对象的属性,例如像

<_pages:Food MyProperty="{Binding SomeViewModelProperty}"/>

不会按预期工作。

【讨论】:

    【解决方案2】:

    为了让您的示例正常工作,您可以将控件绑定到自身。

    <UserControl x:Class="cafeteria._pages.Food"
                 ... DataContext="{Binding RelativeSource={RelativeSource Self}}">
    

    这样您可以直接绑定到包含的属性。为此,您需要将您的 products-field 设置为属性。

    <ItemsControl ItemsSource="{Binding Products}">
    

    就像 Clemens 已经提到的那样,将控件绑定到自身并不是一个好方法,我只是想指出您的示例中缺少什么以便工作。

    或者,您也可以从后面的代码中设置 ItemsSource 属性(即在 ItemsCotrol.Loaded 中)。

    话虽如此,我认为最好将加载数据的逻辑转移到一个单独的类中并从你的窗口绑定结果。

    【讨论】:

    • 虽然DataContext="{Binding RelativeSource={RelativeSource Self}}" 完全没有意义,但ItemsSource="{Binding products}" 也将不起作用,因为products 不是UserControl 的DataContext 中的公共属性,它不应该是一个。
    • @Clemens:我不明白你的意思。通过将控件放入它自己的 DataContext 中,您可以像上面那样引用该属性。请在否决答案之前尝试一下。该示例有效,尽管它肯定不是最漂亮的。
    • 您永远不应该将 UserControl 的 DataContext 设置为自身。这样做会破坏其属性与继承 DataContext 中视图模型对象属性的任何绑定。虽然您可能会争辩说 OP 的控件没有公开任何可绑定属性,但以这种方式设置其 DataContext 仍然是一个不好的建议。干脆不要这样做。
    • 除此之外,该示例不起作用,因为products 是一个字段,而不是一个属性。
    • @Clemens:是的,你说得对。这正是我想用我的答案的最后一句话说的。我只是首先指出了他的样本中缺少的东西以便工作,然后提供了另一种解决方案。我还提到,无论如何,将数据加载到控件内部并不是一个好方法。关于该领域:好的,是的。确实如此。我忽略了这一点。我会将这些信息添加到答案中。感谢您的提示。
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多