简体   繁体   中英

UWP - GridView without Xaml templates

Long story short.

I have an UWP UI which contains a GridView and does not use Xaml. I'd like to display fully code-behind constructed items. No Xaml templates.

I have figured out that the GridView's ChoosingItemContainer event will allow me to create the GridViewItem instances programmatically and even possibly reuse them.

However the custom UI of the items is not actually displayed.

I have noticed that when scrolling a large amount of data the content appears very briefly and then it disappears. I'm guessing that the GridViewItem's Content is being overwritten by some kind of default template. Is there a way to disable this machinery?

More generally speaking, is there a known way to use GridView + Items without Xaml at all?

UPDATE:

Here is a minimal code sample that demonstrates the problem. Place the CustomGridView somewhere in your 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;
        }

    }
}

For you can custom UI by new a style to set.

You can set the resource in xaml,and give it a key ,like "res".

And you can get GridView.Style=(Style)Resources["res"]; in code.

To use style in xaml is a good way.But you can set style in code see:

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

GridView.Style=style ;

The style can be set the GridView and Items just like the xaml.

You have no say that what ui you want that I cant give you a code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM