Skip to content

Commit 94bb119

Browse files
authored
Merge pull request #153 from w-ahmad/template_column_fix
fixed template column template selector issues
2 parents d4b7b25 + 70b86e1 commit 94bb119

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

src/Columns/TableViewColumn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public abstract partial class TableViewColumn : DependencyObject
3434
/// <summary>
3535
/// Refreshes the display element for the cell.
3636
/// </summary>
37-
/// <param name="tableViewCell">The cell for which the element is refreshed.</param>
37+
/// <param name="cell">The cell for which the element is refreshed.</param>
3838
/// <param name="dataItem">The data item associated with the cell.</param>
39-
public virtual void RefreshElement(TableViewCell tableViewCell, object? dataItem) { }
39+
public virtual void RefreshElement(TableViewCell cell, object? dataItem) { }
4040

4141
/// <summary>
4242
/// Updates the state of the element for the cell.

src/Columns/TableViewTemplateColumn.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ public TableViewTemplateColumn()
2222
/// <returns>A ContentControl element.</returns>
2323
public override FrameworkElement GenerateElement(TableViewCell cell, object? dataItem)
2424
{
25-
var contentControl = new ContentControl
26-
{
27-
ContentTemplate = CellTemplate,
28-
ContentTemplateSelector = CellTemplateSelector
29-
};
30-
31-
return contentControl;
25+
var template = CellTemplateSelector?.SelectTemplate(dataItem) ?? CellTemplate;
26+
return (template?.LoadContent() as FrameworkElement)!;
3227
}
3328

3429
/// <summary>
@@ -42,18 +37,18 @@ public override FrameworkElement GenerateEditingElement(TableViewCell cell, obje
4237
{
4338
if (EditingTemplate is not null || EditingTemplateSelector is not null)
4439
{
45-
var contentControl = new ContentControl
46-
{
47-
ContentTemplate = EditingTemplate,
48-
ContentTemplateSelector = EditingTemplateSelector
49-
};
50-
51-
return contentControl;
40+
var template = EditingTemplateSelector?.SelectTemplate(dataItem) ?? EditingTemplate;
41+
return (template?.LoadContent() as FrameworkElement)!;
5242
}
5343

5444
return GenerateElement(cell, dataItem);
5545
}
5646

47+
public override void RefreshElement(TableViewCell cell, object? dataItem)
48+
{
49+
cell.Content = GenerateElement(cell, dataItem);
50+
}
51+
5752
/// <summary>
5853
/// Gets or sets the DataTemplate for the cell content.
5954
/// </summary>

src/TableViewCell.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ internal async void PrepareForEdit()
310310

311311
await Task.Delay(20);
312312

313-
if ((Content ?? ContentTemplateRoot) is UIElement editingElement)
313+
if (Content is UIElement { IsHitTestVisible: true } editingElement)
314314
{
315315
editingElement.Focus(FocusState.Pointer);
316316
}
@@ -368,7 +368,7 @@ private void SetEditingElement()
368368
/// </summary>
369369
internal void RefreshElement()
370370
{
371-
Column?.RefreshElement(this, Content);
371+
Column?.RefreshElement(this, Row?.Content);
372372
}
373373

374374
/// <summary>
@@ -383,18 +383,19 @@ internal void ApplySelectionState()
383383
/// <summary>
384384
/// Applies the current cell state to the cell.
385385
/// </summary>
386-
internal void ApplyCurrentCellState()
386+
internal async void ApplyCurrentCellState()
387387
{
388388
var stateName = IsCurrent ? VisualStates.StateCurrent : VisualStates.StateRegular;
389389
VisualStates.GoToState(this, false, stateName);
390390

391391
if (IsCurrent)
392392
{
393-
Focus(FocusState.Programmatic);
393+
Focus(FocusState.Pointer);
394394

395-
if ((Content ?? ContentTemplateRoot) is UIElement { IsHitTestVisible: true, IsTabStop: true } element)
395+
await Task.Delay(20);
396+
if (Content is UIElement { IsHitTestVisible: true } element)
396397
{
397-
element.Focus(FocusState.Programmatic);
398+
element.Focus(FocusState.Pointer);
398399
}
399400
}
400401
}

0 commit comments

Comments
 (0)