-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathTableViewComboBoxColumn.cs
More file actions
177 lines (156 loc) · 6.41 KB
/
Copy pathTableViewComboBoxColumn.cs
File metadata and controls
177 lines (156 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
namespace WinUI.TableView;
/// <summary>
/// Represents a column in a TableView that displays a ComboBox.
/// </summary>
[StyleTypedProperty(Property = nameof(ElementStyle), StyleTargetType = typeof(TextBlock))]
[StyleTypedProperty(Property = nameof(EditingElementStyle), StyleTargetType = typeof(ComboBox))]
#if WINDOWS
[WinRT.GeneratedBindableCustomProperty]
#endif
public partial class TableViewComboBoxColumn : TableViewBoundColumn
{
/// <summary>
/// Generates a TextBlock element for the cell.
/// </summary>
/// <param name="cell">The cell for which the element is generated.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <returns>A TextBlock element.</returns>
public override FrameworkElement GenerateElement(TableViewCell cell, object? dataItem)
{
var textBlock = new TextBlock
{
Margin = new Thickness(12, 0, 12, 0),
};
if (!string.IsNullOrEmpty(DisplayMemberPath))
{
textBlock.SetBinding(FrameworkElement.DataContextProperty, Binding);
textBlock.SetBinding(TextBlock.TextProperty, new Binding { Path = new PropertyPath(DisplayMemberPath) });
}
else
{
textBlock.SetBinding(TextBlock.TextProperty, Binding);
}
return textBlock;
}
/// <summary>
/// Generates a ComboBox element for editing the cell.
/// </summary>
/// <param name="cell">The cell for which the editing element is generated.</param>
/// <param name="dataItem">The data item associated with the cell.</param>
/// <returns>A ComboBox element.</returns>
public override FrameworkElement GenerateEditingElement(TableViewCell cell, object? dataItem)
{
var comboBox = new ComboBox { HorizontalAlignment = HorizontalAlignment.Stretch };
comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = this, Path = new PropertyPath(nameof(ItemsSource)) });
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;
}
/// <inheritdoc/>
protected internal override object? PrepareCellForEdit(TableViewCell cell, RoutedEventArgs routedEvent)
{
if (cell.Content is ComboBox comboBox)
{
return comboBox.SelectedItem;
}
return base.PrepareCellForEdit(cell, routedEvent);
}
/// <inheritdoc/>
protected internal override void EndCellEditing(TableViewCell cell, object? dataItem, TableViewEditAction editAction, object? uneditedValue)
{
if (cell.Content is ComboBox comboBox)
{
if (editAction == TableViewEditAction.Commit)
{
var bindingExpression = comboBox.GetBindingExpression(Selector.SelectedItemProperty);
bindingExpression?.UpdateSource();
}
}
}
/// <summary>
/// Gets or sets the items source for the ComboBox.
/// </summary>
public object? ItemsSource
{
get => GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
/// <summary>
/// Gets or sets the path to the display member for the ComboBox.
/// </summary>
public string? DisplayMemberPath
{
get => (string?)GetValue(DisplayMemberPathProperty);
set => SetValue(DisplayMemberPathProperty, value);
}
/// <summary>
/// Gets or sets the path to the selected value for the ComboBox.
/// </summary>
public string? SelectedValuePath
{
get => (string?)GetValue(SelectedValuePathProperty);
set => SetValue(SelectedValuePathProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the ComboBox is editable.
/// </summary>
public bool IsEditable
{
get => (bool)GetValue(IsEditableProperty);
set => SetValue(IsEditableProperty, value);
}
/// <summary>
/// Gets or sets the binding for the text property of the ComboBox.
/// </summary>
public virtual Binding? TextBinding
{
get;
set
{
field = value;
field?.Mode = BindingMode.TwoWay;
}
}
/// <summary>
/// Gets or sets the binding for the selected value property of the ComboBox.
/// </summary>
public virtual Binding? SelectedValueBinding
{
get;
set
{
field = value;
field?.Mode = BindingMode.TwoWay;
}
}
/// <summary>
/// Identifies the SelectedValuePath dependency property.
/// </summary>
public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register(nameof(SelectedValuePath), typeof(string), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
/// <summary>
/// Identifies the DisplayMemberPath dependency property.
/// </summary>
public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register(nameof(DisplayMemberPath), typeof(string), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
/// <summary>
/// Identifies the ItemsSource dependency property.
/// </summary>
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(TableViewComboBoxColumn), new PropertyMetadata(default));
/// <summary>
/// Identifies the IsEditable dependency property.
/// </summary>
public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register(nameof(IsEditable), typeof(bool), typeof(TableViewComboBoxColumn), new PropertyMetadata(false));
}