Skip to content

Commit

Permalink
Merge pull request #18 from editable-combobox
Browse files Browse the repository at this point in the history
introduced new properties to ComboBoxColumn
  • Loading branch information
w-ahmad authored Jun 3, 2024
2 parents ebef34d + 3faf17a commit b01820f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
using CommunityToolkit.WinUI;

namespace WinUI.TableView;

public class TableViewCell : ContentControl
{
private Lazy<FrameworkElement> _element = null!;
private Lazy<FrameworkElement> _editingElement = null!;

public TableViewCell()
{
DefaultStyleKey = typeof(TableViewCell);
Expand Down Expand Up @@ -84,6 +82,12 @@ protected override async void OnLostFocus(RoutedEventArgs e)

await Task.Delay(20);

var focusedElement = FocusManager.GetFocusedElement(XamlRoot) as FrameworkElement;
if (focusedElement?.FindAscendantOrSelf<TableViewCell>() == this)
{
return;
}

if (VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).Any())
{
return;
Expand All @@ -100,7 +104,7 @@ private void SetElement()
}
else
{
Content = _element.Value;
Content = Column.GenerateElement();
}
}

Expand All @@ -112,16 +116,14 @@ private void SetEditingElement()
}
else if (Column is not null)
{
Content = _editingElement.Value;
Content = Column.GenerateEditingElement();
}
}

private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewCell cell && e.NewValue is TableViewColumn column)
{
cell._element = new Lazy<FrameworkElement>(column.GenerateElement());
cell._editingElement = new Lazy<FrameworkElement>(column.GenerateEditingElement());
cell.SetElement();
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/WinUI.TableView/TableViewComboBoxColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace WinUI.TableView;

public class TableViewComboBoxColumn : TableViewBoundColumn
{
private Binding? _textBinding;
private Binding? _selectedValueBinding;

public override FrameworkElement GenerateElement()
{
var textBlock = new TextBlock
Expand All @@ -25,6 +28,17 @@ public override FrameworkElement GenerateEditingElement()
comboBox.SetBinding(Selector.SelectedValuePathProperty, new Binding { Source = this, Path = new PropertyPath(nameof(SelectedValuePath)) });
comboBox.SetBinding(ItemsControl.DisplayMemberPathProperty, new Binding { Source = this, Path = new PropertyPath(nameof(DisplayMemberPath)) });
comboBox.SetBinding(Selector.SelectedItemProperty, Binding);
comboBox.SetBinding(ComboBox.IsEditableProperty, new Binding { Source = this, Path = new PropertyPath(nameof(IsEditable)) });

if (TextBinding is not null)
{
comboBox.SetBinding(ComboBox.TextProperty, TextBinding);
}

if (SelectedValueBinding is not null)
{
comboBox.SetBinding(Selector.SelectedValueProperty, SelectedValueBinding);
}

return comboBox;
}
Expand All @@ -47,7 +61,40 @@ public string SelectedValuePath
set => SetValue(SelectedValuePathProperty, value);
}

public bool IsEditable
{
get => (bool)GetValue(IsEditableProperty);
set => SetValue(IsEditableProperty, value);
}

public virtual Binding TextBinding
{
get => _textBinding!;
set
{
_textBinding = value;
if (_textBinding is not null)
{
_textBinding.Mode = BindingMode.TwoWay;
}
}
}

public virtual Binding SelectedValueBinding
{
get => _selectedValueBinding!;
set
{
_selectedValueBinding = value;
if (_selectedValueBinding is not null)
{
_selectedValueBinding.Mode = BindingMode.TwoWay;
}
}
}

public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register(nameof(SelectedValuePath), typeof(string), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register(nameof(DisplayMemberPath), typeof(string), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register(nameof(IsEditable), typeof(bool), typeof(TableViewComboBoxColumn), new PropertyMetadata(false));
}

0 comments on commit b01820f

Please sign in to comment.