UWP - 没有Xaml模板的GridView

时间:2017-03-17 04:28:29

标签: c# uwp

长话短说。

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

我已经发现GridView的ChoosingItemContainer事件将允许我以编程方式创建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;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过新设置的样式自定义UI。

您可以在xaml中设置资源,并为其指定密钥,例如&#34; res&#34;。

你可以得到GridView.Style =(样式)资源[&#34; res&#34;];在代码中。

在xaml中使用样式是一种很好的方法。但是你可以在代码中设置样式,参见:

$literal

可以将样式设置为GridView和Items,就像xaml一样。

你没有说你想要的是什么我不能给你一个代码。

相关问题