【问题标题】:UWP - GridView without Xaml templatesUWP - 没有 Xaml 模板的 GridView
【发布时间】:2017-03-17 04:28:29
【问题描述】:

长话短说。

我有一个包含 GridView 且不使用 Xaml 的 UWP UI。我想显示完全隐藏代码的构造项目。没有 Xaml 模板。

我发现 GridView 的 ChooseItemContainer 事件将允许我以编程方式创建 GridViewItem 实例,甚至可能重用它们。

但是项目的自定义 UI 并没有实际显示。

我注意到,当滚动大量数据时,内容会非常短暂地出现,然后就消失了。我猜 GridViewItem 的内容被某种默认模板覆盖。有没有办法禁用这个机器?

更一般地说,有没有一种已知的方法可以在没有 Xaml 的情况下使用 GridView + Items?

更新:

这是一个演示问题的最小代码示例。 将 CustomGridView 放置在 UI 中的某个位置。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace MyApp
{
    // Some kind of data object
    public class MyData
    {
        public string MyProperty;
    }

    // A custom GridViewItem
    public class MyGridViewItem : GridViewItem
    {
        private TextBox mTextBox;

        public MyGridViewItem()
        {
            mTextBox = new TextBox();

            mTextBox.Width = 100;
            mTextBox.Height = 100;

            Content = mTextBox;

            // Make the items visible at all: use red background
            Background = new SolidColorBrush(Color.FromArgb(255,255,0,0));
        }

        public void SetData(MyData d)
        {
            mTextBox.Text = d.MyProperty;

            // Content seems to be always reset to the data object itself.
            Content = mTextBox;

            // With the following line the contents appear briefly while the view is scrolling.
            // Without this line the contents don't appear at all
            Template = null;
        }
    }

    // Custom grid. No Xaml.
    public class CustomGridView : GridView
    {
        public CustomGridView()
        {
            this.ChoosingItemContainer += CustomGridView_ChoosingItemContainer;

            // Create some data to show.
            CollectionViewSource s = new CollectionViewSource();
            ObservableCollection<MyData> oc = new ObservableCollection<MyData>();

            for(int i = 0;i < 10000;i++)
            {
                MyData d = new MyData();
                d.MyProperty = i.ToString();
                oc.Add(d);
            }

            s.Source = oc;
            ItemsSource = s.View;
        }

        private void CustomGridView_ChoosingItemContainer(ListViewBase sender,ChoosingItemContainerEventArgs args)
        {
            // Unchecked cast, but for the sake of simplicity let's assume it always works.
            MyData d = (MyData)args.Item;

            MyGridViewItem it = null;

            if((args.ItemContainer != null) && (args.ItemContainer.GetType() == typeof(MyGridViewItem)))
                it = (MyGridViewItem)args.ItemContainer;
            else
                it = new MyGridViewItem();

            it.SetData(d);

            args.ItemContainer = it;
            args.IsContainerPrepared = true;

            // This should probably go elsewhere, but for simplicity it's here :)
            ((ItemsWrapGrid)ItemsPanelRoot).ItemWidth = 100;
            ((ItemsWrapGrid)ItemsPanelRoot).ItemHeight = 100;
        }

    }
}

【问题讨论】:

  • 您能说明如何将GridViewItems 添加到GridView 吗?如果没有您的代码,就无法弄清楚问题是什么以及为什么会发生。请看minimal reproducible example
  • 我已经用一个最小的例子更新了这个问题。

标签: c# uwp


【解决方案1】:

因为您可以通过新的样式来自定义 UI。

您可以在 xaml 中设置资源,并给它一个键,如“res”。

你可以得到 GridView.Style=(Style)Resources["res"];在代码中。

在xaml中使用样式是一个好方法。但是你可以在代码中设置样式见:

Style style = new Style(typeof (GridView));
style.Setters.Add(new Setter(GridView.Property, xx));

GridView.Style=style ;

样式可以像xaml一样设置GridView和Items。

你没有说你想要什么ui,我不能给你代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多