在运行时创建WPF ItemTemplate中强夯 [英] Create WPF ItemTemplate DYNAMICALLY at runtime

查看:92
本文介绍了在运行时创建WPF ItemTemplate中强夯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时,我想在一个WPF ListView中动态生成网格列(或另一个显示布局)。我不知道前手的数量和列的名称

At run time I want to dynamically build grid columns (or another display layout) in a WPF ListView. I do not know the number and names of the columns before hand.

我希望能够做到:

  MyListView.ItemSource = MyDataset;

  MyListView.CreateColumns();

I want to be able to do:
MyListView.ItemSource = MyDataset;
MyListView.CreateColumns();

推荐答案

我想尝试以下方法:

A)你需要有列表框中显示网格视图 - 我相信这一点,你已经做了结果
B)定义样式GridViewColumnHeader:

A) you need to have the list box display grid view - i believe this you've done already
B) define a style for GridViewColumnHeader:

        <Style TargetType="{x:Type GridViewColumnHeader}" x:Key="gridViewColumnStyle">
            <EventSetter Event="Click" Handler="OnHeaderClicked"/>
            <EventSetter Event="Loaded" Handler="OnHeaderLoaded"/>
        </Style>

在我的情况,我有一大堆设置其他属性,但在基本场景 - 你需要加载的事件。点击 - 如果你想添加排序和过滤功能,这是非常有用的。

in my case, i had a whole bunch of other properties set, but in the basic scenario - you'd need Loaded event. Clicked - this is useful if you want to add sorting and filtering functionality.

在您的列表视图code C),模板绑定你的GridView:

C) in your listview code, bind the template with your gridview:

    public MyListView()
    {
        InitializeComponent();
        GridView gridViewHeader = this.listView.View as GridView;
        System.Diagnostics.Debug.Assert(gridViewHeader != null, "Expected ListView.View should be GridView");
        if (null != gridViewHeader)
        {
            gridViewHeader.ColumnHeaderContainerStyle = (Style)this.FindResource("gridViewColumnStyle");
        }
    }

D),然后在你OnHeaderLoaded处理,你可以设置基于列的数据一个正确的模板

D) then in you OnHeaderLoaded handler, you can set a proper template based on the column's data

    void OnHeaderLoaded(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader header = (GridViewColumnHeader)sender;
        GridViewColumn column = header.Column;

//选择在这里适用的数据模板。

//select and apply your data template here.

        e.Handled = true;
    }

E)我想你想也需要掌握的ItemsSource依赖财产的所有权和处理它的更改事件。

E) I guess you'd need also to acquire ownership of ItemsSource dependency property and handle it's changed event.

            ListView.ItemsSourceProperty.AddOwner(typeof(MyListView), new PropertyMetadata(OnItemsSourceChanged));

        static void OnItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            MyListView view = (MyListView)sender;
            //do reflection to get column names and types
            //and for each column, add it to your grid view:
            GridViewColumn column = new GridViewColumn();
            //set column properties here...
            view.Columns.Add(column);
        }

在GridViewColumn类本身没有太多的属性,所以你可能要在那里添加一些信息使用附加属性 - 即像唯一的列标签 - 标题很可能将用于本地化,你会不会在这一个中继。

the GridViewColumn class itself doesn't have much properties, so you might want to add some information there using attached properties - i.e. like unique column tag - header most likely will be used for localization, and you will not relay on this one.

在一般情况下,这种做法,即使相当复杂的,可以让你轻松地扩展您的列表视图功能。

In general, this approach, even though quite complicated, will allow you to easily extend your list view functionality.

这篇关于在运行时创建WPF ItemTemplate中强夯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆