Skip to content

Add Command & CommandParameter Bindings to TableViewBoundColumns #303

@ProphetLamb

Description

@ProphetLamb

Is your feature request related to a problem? Please describe.
Unable to react to edits in the table view using ICommand interface.

Describe the solution you'd like
Add Command and CommandParameter properties bound to the generated FrameworkElement.

Describe alternatives you've considered
Manually deriving my own TableViewBoundColumn and implementing it myself.

Additional context

<controls:TableViewCheckBoxColumn
  Binding="{Binding IsSigned, Mode=TwoWay}"
  Command="{Binding  DataContext.ToggleSigned, ElementName=OrderSign}"
  x:Uid="ComponentIsSignedColumn"
  d:DataContext="{d:DesignInstance models:OperationComponent}"
  IsReadOnly="False" />
public sealed partial class TableViewCheckBoxColumn : WinUI.TableView.TableViewCheckBoxColumn
{
    public override FrameworkElement GenerateElement(TableViewCell cell, object? dataItem)
    {
        var element = base.GenerateElement(cell, dataItem);
        if (element is not CheckBox cb)
        {
            return element;
        }

        cb.Loaded += CheckBoxLoaded;
        return element;
    }

    private static void CheckBoxLoaded(object sender, RoutedEventArgs e)
    {
        var cb = sender as CheckBox;
        var cell = cb?.FindFirstAncestor<TableViewCell>();
        var column = cell?.Column as TableViewCheckBoxColumn;
        if (column?.Command is { } command)
        {
            cb?.SetBinding(ButtonBase.CommandProperty, command);
        }

        if (column?.CommandParameter is { } commandParameter)
        {
            cb?.SetBinding(ButtonBase.CommandParameterProperty, commandParameter);
        }
        else
        {
            cb?.CommandParameter = cell?.DataContext;
            cell?.DataContextChanged += CellOnDataContextChanged;
        }
    }

    private static void CellOnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
    {
        var cell = sender as TableViewCell;
        var cb = cell?.Content as CheckBox;
        cb?.CommandParameter = cell?.DataContext;
    }

    public BindingBase? Command { get; set; }
    public BindingBase? CommandParameter { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions