Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a 'CellTemplateSelector' similar property in community's DataGrid control #2958

Closed
xiedongweo opened this issue Jun 28, 2019 · 6 comments

Comments

@xiedongweo
Copy link

I'm submitting a...

Feature request (UserVoice request: https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/38030980-need-a-celltemplateselector-relevant-things-in-c)

Current behavior

There's no sucn a property.

Expected behavior

I want such a 'CellTemplateSelector' property. Then, I can choose different dataTemplate for the CellTemplate by my own DataTemplateSelector.

Minimal reproduction of the problem with instructions

Environment

Nuget Package(s): 

Package Version(s): 

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] Insider Build (xxxxx)

Device form factor:
- [x ] Desktop
- [ ] Mobile
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [ ] 2017 (version: )
- [ ] 2017 Preview (version: )

@ghost ghost added the needs triage 🔍 label Jun 28, 2019
@xiedongweo xiedongweo changed the title Need a 'CellTemplateSelector' relevant things in community's DataGrid control Need a 'CellTemplateSelector' similar property in community's DataGrid control Jun 28, 2019
@PaulaScholz
Copy link

I want this too.

@michael-hawker michael-hawker added the DataGrid 🔠 Issues on DataGrid control label Jul 30, 2019
@xiedongweo
Copy link
Author

xiedongweo commented Jul 31, 2019

@PaulaScholz Since the WindowsCommunityToolkit is open source. I spent some time to do some customizations for the DataGrid. Since I have no permission to push the changes to master, so I post the code here.

Open the 'Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.csproj' project and open the 'DataGridTemplateColumn.cs'. Add a 'cellTemplateSelector' property for it like the following:

private DataTemplateSelector _cellTemplateSelector;

public DataTemplateSelector CellTemplateSelector {
        get { return _cellTemplateSelector; }
        set {
            if (_cellTemplateSelector != value)
            {
                _cellTemplateSelector = value;
            }
        }
    }

Edit the 'GenerateElement' method like the following:

 protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        if (this.CellTemplate != null)
        {
            return this.CellTemplate.LoadContent() as FrameworkElement;
        }

        if (this.CellEditingTemplate != null)
        {
            return this.CellEditingTemplate.LoadContent() as FrameworkElement;
        }

        if (this.CellTemplateSelector != null)
        {
            return this.CellTemplateSelector.SelectTemplate(dataItem).LoadContent() as FrameworkElement;
        }

        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            return null;
        }
        else
        {
            throw DataGridError.DataGridTemplateColumn.MissingTemplateForType(typeof(DataGridTemplateColumn));
        }
    }

Then, you could compile the whole project and use the custom DataGrid control in your UWP project.

<Page.Resources>
    <DataTemplate x:DataType="local:Person" x:Key="template1">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Background="Red">
            <TextBlock Padding="5,0,5,0"
                        Text="{x:Bind FirstName}"/>
            <TextBlock Text="{x:Bind LastName}"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:DataType="local:Person" x:Key="template2">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Background="Green">
            <TextBlock Padding="5,0,5,0"
                        Text="{x:Bind FirstName}"/>
            <TextBlock Text="{x:Bind LastName}"/>
        </StackPanel>
    </DataTemplate>

    <local:CustomCellTemplateSelector x:Key="CellTemplateSelector" template1="{StaticResource template1}" template2="{StaticResource template2}"></local:CustomCellTemplateSelector>
</Page.Resources>
<Grid>
    <DG:DataGrid x:Name="dataGrid1" 
Height="600" Margin="12"
AutoGenerateColumns="False"
ItemsSource="{x:Bind Persons}">
        <DG:DataGrid.Columns>
            <!-- Name Column -->
            <DG:DataGridTemplateColumn Header="Name" CellTemplateSelector="{StaticResource CellTemplateSelector}">
            </DG:DataGridTemplateColumn>
        </DG:DataGrid.Columns>
    </DG:DataGrid>
</Grid>

@RBrid
Copy link
Contributor

RBrid commented Oct 10, 2019

I believe that row recycling is throwing a wrench into that idea (it's a DataGrid feature that improves perf). A recycled row by default will not trigger a call to GenerateElement. So recycled rows would normally appear using incorrect cell templates. There is a trick to turn off a row's recycling. See private void UnloadRow(DataGridRow dataGridRow) in DataGridRows.cs:

            // Don't recycle if the row has a custom Style set
            recycleRow &= dataGridRow.Style == null || dataGridRow.Style == this.RowStyle;

You need to set the DataGridRow.Style property on all rows. This is to prevent them from being recycled, but it has perf implications.

@ghost
Copy link

ghost commented Oct 18, 2019

This issue has been marked as "Needs: Attention 👋" due to no activity for 7 days. Please triage and assign the issue so the fix can be established.

@ghost ghost added the needs attention 👋 label Oct 18, 2019
@ghost
Copy link

ghost commented Oct 25, 2019

This issue has been marked as "Needs: Attention 👋" due to no activity for 7 days. Please triage the issue so the fix can be established.

@harinikmsft
Copy link
Contributor

We are not adding features to this DataGrid at this time. Please use @RBrid 's workaround above. If you would like to see this property in the newly proposed WinUI DataGrid, please add a comment in the WinUI discussion issue.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants