简体   繁体   English

在UWP运行时重新生成DataGrid列

[英]re-generate DataGrid columns at run time in UWP

I use MVVM pattern in UWP app. 我在UWP应用中使用MVVM模式。 VM defines a list of DataGrid columns. VM定义了DataGrid列的列表。 The following code-behind I use for generating columns during UserControl load: 我在UserControl加载期间用于生成列的以下代码隐藏:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                if (ViewModel.PresentColumns.Contains(e.PropertyName.ToLower()))
                {
                    var templateName = e.PropertyName + "DataTemplate";
                    var templateColumn = new DataGridTemplateColumn();
                    templateColumn.CellTemplate = Application.Current.Resources[templateName] as DataTemplate;
                    templateColumn.Header = e.PropertyName;
                    e.Column = templateColumn;
                }
                else
                {
                    e.Cancel = true;
                }
            }

XAML: XAML:

<msgrid:DataGrid  Name="GridTargets" Grid.Row="1"
                 Grid.Column="0"
                 ItemsSource="{x:Bind ViewModel.TargetSteps}"
                 SelectedItem="{x:Bind ViewModel.SelectedTargetStep, Mode=TwoWay}"
                 AutoGenerateColumns="True"
                 GridLinesVisibility="All"
                 CanUserResizeColumns="False"
                 AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"/>

VM: VM:

  public IEnumerable<string> PresentColumns { get; private set; }

I need 'replace' the grid during run time. 我需要在运行时“替换”网格。 All "available grids" (lets say 10 grids overall) are predefined; 所有“可用网格”(假设总共10个网格)都是预定义的; the ViewModel.PresentColumns is generated at run time for every "available grid". 在运行时为每个“可用网格”生成ViewModel.PresentColumns。 I think to re-generate columns during run. 我认为要在运行期间重新生成列。 DataTemplates for all available columns are predefined in global resources. 在全局资源中预定义了所有可用列的DataTemplates。 Is it doable or there is another architectural solution? 是可行的,还是有另一种架构解决方案? It is not desirable keeping few grids in the UserControl. 不希望在UserControl中保留几个网格。

That is DataGrid form Windows Community Toolkit - ns Microsoft.Toolkit.Uwp.UI.Controls. 那就是Windows社区工具包中的DataGrid-ns Microsoft.Toolkit.Uwp.UI.Controls。 The initial task is: dynamicaly generate DataGrid columns from VM; 初始任务是:从VM动态生成DataGrid列; constrain of the MS DataGrid - property name, which define a grid column has to be defined in compile time. MS DataGrid的约束-属性名称,用于定义网格列的必须在编译时定义。 I have Class Foo with 100 properties. 我有100个属性的Foo类。 The collection of the Foo is a ItemsSource of DataGrid. Foo的集合是DataGrid的ItemsSource。 Each property is a custom object and define columns in the grid. 每个属性都是一个自定义对象,并在网格中定义列。 There is DataTemplate for every property in global resources for presentation data in the grid. 全局资源中的每个属性都有DataTemplate,用于表格中的表示数据。 I need show different groups of properties in one view, groups toggle instantly from VM. 我需要在一个视图中显示不同的属性组,组可以从VM立即切换。 I see the following workarrounds: 1. Instantly 're-generate' just grid (questionable) 2. Wrapp grid in UserControl, load-unload UserControl, that will force generate new grid every time (doable). 我看到以下工作环境:1.立即“重新生成”正则网格(可疑)2.在UserControl中打包Wrapp网格,卸载UserControl,这将每次都强制生成新网格(可行)。 3.Generate grid with all 100 columns and hide/show columns on demend from VM(doable, not desirable). 3.生成具有所有100列的网格,并根据VM的要求隐藏/显示列(可行,不理想)。

Actually I realise that I am looking for the way to trigger the AutoGeneratingColumn event; 实际上,我意识到我正在寻找触发AutoGeneratingColumn事件的方式。 changing the ItemsSource property works for this task. 更改ItemsSource属性可用于此任务。 Thus, I am going 'artificially' change the source in VM. 因此,我将“人为地”更改VM中的源。 I consider your comment as answer on my question. 我认为您的评论是对我问题的回答。 Thank you Barry. 谢谢你,巴里。

After the confirm from the original poster. 经原海报确认后。 The OP want to look for the way to trigger the AutogeneratingColumn event. OP希望寻找触发AutogeneratingColumn事件的方式。 This has been wrote in the official doc . 这已经写在官方文档中 Where you can see the following: 在这里您可以看到以下内容:

"The AutoGeneratingColumn event occurs one time for each public, non-static property in the bound data type when the ItemsSource property is changed and the AutoGenerateColumns property is true." “当ItemsSource属性更改并且AutoGenerateColumns属性为true时,绑定数据类型中的每个公共的非静态属性都会发生一次AutoGeneratingColumn事件。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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