简体   繁体   English

确定哪个单元格正在GridViewRowPresenter中调用SelectTemplate

[英]Determine what cell is calling SelectTemplate in GridViewRowPresenter

I'm attempting to use the GridViewRowPresenter to be the ItemsPresenter in a TreeListView. 我试图将GridViewRowPresenter用作TreeListView中的ItemsPresenter。 The catch is I want to be able to determine the DataTemplates for each cell in the grid at run time. 我要抓住的是,我希望能够在运行时确定网格中每个单元的数据模板。 I've been using a DataTemplateSelector to choose the template but in order to pick the right template for the right column I need to determine which column is the current column in the call to SelectTemplate. 我一直在使用DataTemplateSelector选择模板,但是为了为正确的列选择正确的模板,我需要确定对SelectTemplate的调用中的当前列是哪一列。 What I've been doing is assuming that SelectTemplate gets called sequentially. 我一直在做的是假设SelectTemplate被顺序调用。 In other words if I have 4 columns the first call would be for the first column the second for the next and so on. 换句话说,如果我有4列,则第一个调用将是第一列,第二个是下一个,依此类推。 So I could have a list of DataTemplates in the ContentTemplateSelector and simply return the appropriate template, after 4 calls reset the index and return the first template again. 因此,在4次调用重置索引并再次返回第一个模板之后,我可以在ContentTemplateSelector中有一个DataTemplates列表,然后简单地返回适当的模板。

This mostly works, however, I have found that after a certain number of rows I can no longer count on the calls to SelectTemplate to come in a consistent pattern. 这在大多数情况下都是有效的,但是,我发现经过一定数量的行之后,我不能再依靠对SelectTemplate的调用来保持一致的模式了。

Does anyone have any advise as to how to achieve this? 有没有人建议如何实现这一目标? Is there a way to determine the Cell in question in the SelectTemplate call? 有没有办法确定SelectTemplate调用中的相关单元格?

I have been playing with WPF DataGrid and I needed to do something similar. 我一直在使用WPF DataGrid,并且需要做类似的事情。 I tried to do most of it in XAML but you could certainly do it in the code-behind as well. 我尝试在XAML中完成大多数操作,但是您当然也可以在后面的代码中进行操作。

In my case, I wanted to choose a template for each cell in a DataGrid based on information within the data being displayed. 就我而言,我想根据所显示数据中的信息为DataGrid中的每个单元选择一个模板。 My DataGrid shows chunks of data that are all put together to create a Serial Command. 我的DataGrid显示了全部组装在一起以创建串行命令的数据块。 I wanted the ability to select the Edit / Display template based on the type of data each "SerialBlock" contains. 我希望能够根据每个“ SerialBlock”包含的数据类型选择“编辑/显示”模板。

public class ColumnTemplateSelector : DataTemplateSelector
{
    public DataTemplate ColorTemplate { get; set; }
    public DataTemplate RawBytesTemplate { get; set; }
    public DataTemplate AddressTemplate { get; set; }
    public DataTemplate CommandTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {

        SerialBlock command = item as SerialBlock;
        if (null != command)
        {
            switch (command.BlockType)
            {
                case EnumBlockType.Address:
                    return AddressTemplate;
                case EnumBlockType.Color:
                    return ColorTemplate;
                case EnumBlockType.Command:
                    return CommandTemplate;
                case EnumBlockType.RawBytes:
                    return RawBytesTemplate;

            }
        }
        return base.SelectTemplate(item, container);

    }

}

In the XAML, I use the following to create a column... (I have several more as well) 在XAML中,我使用以下内容创建一列...(还有更多内容)

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplateSelector>
        <src:ColumnTemplateSelector 
            ColorTemplate="{StaticResource ColorEditTemplate}"
            AddressTemplate="{StaticResource AddressEditTemplate}"
            RawBytesTemplate="{StaticResource RawBytesEditTemplate}"
            CommandTemplate="{StaticResource CommandEditTemplate}">
        </src:ColumnTemplateSelector>
    </DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>

Then I create the Templates like this... (of course, I have have several more) 然后我像这样创建模板...(当然,我还有其他几个)

    <DataTemplate x:Key="AddressTemplate">
        <Border Padding="3" Background="Green">
            <TextBlock Text="{Binding Path=BlockText}" />
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="AddressEditTemplate">
        <Border Padding="3" Background="Green">
            <TextBlock Text="{Binding Path=BlockText}" />
        </Border>
    </DataTemplate>

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

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