简体   繁体   English

带有AutoGenerateColumns的WPF Datagrid中的ItemTemplateSelector

[英]ItemTemplateSelector in WPF Datagrid with AutoGenerateColumns

In our data grid we're using an ItemTemplateSelector to switch between two data templates based on the data bound to a particular cell. 在我们的数据网格中,我们使用ItemTemplateSelector根据绑定到特定单元格的数据在两个数据模板之间切换。

As the number of columns depends on the current data set we're using AutoGenerateColumns in our DataGrid . 由于列数取决于当前数据集,我们在DataGrid使用AutoGenerateColumns

It appears that this particular combination does not work well - 似乎这种特殊组合效果不佳 -
the template selector isn't even called. 甚至没有调用模板选择器。

Can we use the template selector in a data grid where columns are created automatically? 我们可以在自动创建列的数据网格中使用模板选择器吗?

More specifically: Is this possible using XAML only w/o putting logic into the code-behind file or using custom behaviours? 更具体地说:这是否可以使用XAML而不将逻辑放入代码隐藏文件或使用自定义行为?

Our data grid definition is fairly trivial: 我们的数据网格定义相当简单:

 <DataGrid
     ItemTemplateSelector="{StaticResource myCustomDataTemplateSelector}"
     ItemsSource="{Binding MyData}">
     <DataGrid.Columns>
     </DataGrid.Columns>
 </DataGrid>

The item template selector definition : 项目模板选择器定义

<UserControl.Resources>
    <ResourceDictionary>
        <helpers:CustomDataTemplateSelector x:Key="myCustomDataTemplateSelector"/>
    </ResourceDictionary>
</UserControl.Resources>

First, 第一,

ItemTemplate and ItemTemplateSelector are inherited properties which are purposely ignored in the DataGrid since they don't really apply to DataGrid in the way that they were meant to in ItemsControl. ItemTemplate和ItemTemplateSelector是在DataGrid中故意忽略的继承属性,因为它们并不像ItemsControl中那样真正应用于DataGrid。

Secondly, if you mean that you want to modify cell templae based on its value, you are looking for CellTemplateSelector , on a DataGridTemplateColumn . 其次,如果您想根据其值修改单元格模板,则需要在DataGridTemplateColumn上查找CellTemplateSelector

However, when you auto-generate columns, it already chooses the underlying types automatically. 但是,当您自动生成列时,它已自动选择基础类型。

You can override that behavior in GeneratingColumns event. 您可以在GeneratingColumns事件中覆盖该行为。

See this: Force DataTemplateCell with CellTemplateSelector in WPF DataGrid Autogenerated columns 请参阅: 在WPF DataGrid自动生成列中使用CellTemplateSelector强制DataTemplateCell

If you need more control, you might want to take a look at https://zamjad.wordpress.com/2011/09/17/datagrid-with-dynamic-columns-revisited/ 如果您需要更多控制权,您可能需要查看https://zamjad.wordpress.com/2011/09/17/datagrid-with-dynamic-columns-revisited/

I recently ran into this problem and solved it this way: 我最近遇到了这个问题并以这种方式解决了它:

we can inherit the class DataGridBoundColumn 我们可以继承类DataGridBoundColumn

public class DataGridProcessContainerColumn : DataGridBoundColumn
{
    public DataTemplate ContentTemplate { get; set; }

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        throw new NotImplementedException();
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var control = new ContentControl();
        control.ContentTemplate = ContentTemplate;
        BindingOperations.SetBinding(control, ContentControl.ContentProperty, Binding);
        return control;
    }
}

Next, in the event handler, where the column is generated, I do: 接下来,在生成列的事件处理程序中,我执行以下操作:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    DataTemplate template = null;

    // Four lines below replace the DataTemplateSelector
    // You need to select the desired template according to your conditions
    if (e.PropertyType.IsAssignableFrom(typeof(IEnumerable<MyClass2>)))
        template = (DataTemplate)Resources["MyClass2CollectionTemplate"];
    else if (e.PropertyType.IsAssignableFrom(typeof(MyClass2)))
        template = (DataTemplate)Resources["MyClass2Template"];

    if (template != null)
    {
        var col = new DataGridProcessContainerColumn();
        col.Binding = (e.Column as DataGridBoundColumn).Binding;
        col.ContentTemplate = template;
        col.Header = e.Column.Header;
        e.Column = col;
    }
}

In the resources of the window I have the corresponding templates. 在窗口的资源中,我有相应的模板。

It is possible to do through DataTemplateSelector, but there was no time. 可以通过DataTemplateSelector完成,但没有时间。

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

相关问题 带有 AutoGenerateColumns 和 Cellbackground 的 C# wpf Datagrid - C# wpf Datagrid with AutoGenerateColumns and Cellbackground Wpf-Datagrid和ObservableCollection-AutoGenerateColumns? - Wpf-Datagrid and ObservableCollection-AutoGenerateColumns? 当AutoGenerateColumns为可空的bool时,WPF DataGrid强制绑定DataGridCheckBoxColumn - WPF DataGrid force binding DataGridCheckBoxColumn when AutoGenerateColumns for nullable bool 当CustomControl DataGrid(WPF Datagrid)中的AutoGenerateColumns = false时,DataGrid.Columns中提到的列不可见 - Columns mentioned in DataGrid.Columns is not visible when AutoGenerateColumns=false in CustomControl DataGrid (WPF Datagrid) WPF Treeview HierarchicalDataTemplate ItemTemplateSelector - WPF Treeview HierarchicalDataTemplate ItemTemplateSelector WPF在DataTemplates中改进了ItemTemplateSelector - WPF emended ItemTemplateSelector in a DataTemplates 如果AutoGenerateColumns为false,则DataGrid无法正确绑定到DataGrid - DataGrid does not properly bind to DataGrid if AutoGenerateColumns is false 仅当AutoGenerateColumns = true时,Datagrid才能工作 - Datagrid only works when AutoGenerateColumns = true 将删除按钮添加到设置为AutoGenerateColumns的datagrid吗? - Add delete button to datagrid that are set to AutoGenerateColumns? 当 AutoGenerateColumns=true WPF 时绑定 DataGridComboBoxColumn - Binding DataGridComboBoxColumn when AutoGenerateColumns=true WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM