1
ļ»æ// Copyright Ā© Microsoft Corporation.
2
// This source is subject to the Microsoft Source License for Silverlight Controls (March 2008 Release).
3
// Please see http://go.microsoft.com/fwlink/?LinkID=111693 for details.
4
// All other rights reserved.
6
using System.Windows.Data;
7
using System.Windows.Markup;
8
using System.Windows.Controls;
10
namespace System.Windows.Controlsb1
12
public class DataGridCheckBoxColumn : DataGridBoundColumnBase
16
private const string DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName = "CheckBoxContentBinding";
17
private const string DATAGRIDCHECKBOXCOLUMN_isThreeStateName = "IsThreeState";
23
private Binding _checkBoxContentBinding; //
24
private CheckBox _editingCheckBox;
25
private bool _isThreeState; //
26
// Used to set the Style on our ReadOnlyCheckBox since Styles don't inherit in Silverlight
27
private static Style _readOnlyCheckBoxStyle = InitializeCheckBoxStyle();
31
public DataGridCheckBoxColumn()
33
this.ElementStyle = _readOnlyCheckBoxStyle;
36
#region Dependency Properties
85
#endregion Dependency Properties
87
#region Public Properties
89
public Binding CheckBoxContentBinding
93
return this._checkBoxContentBinding;
97
if (this._checkBoxContentBinding != value)
99
this._checkBoxContentBinding = value;
100
UpdateElements(DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName);
105
public bool IsThreeState
109
return this._isThreeState;
113
if (this._isThreeState != value)
115
this._isThreeState = value;
116
UpdateElements(DATAGRIDCHECKBOXCOLUMN_isThreeStateName);
121
#endregion Public Properties
123
#region Public Methods
125
public override void CancelCellEdit(object uneditedValue)
127
if (this._editingCheckBox != null)
129
this._editingCheckBox.IsChecked = (bool?)uneditedValue;
133
public override object PrepareCellEdit(DataGridEditingTriggerInfo editingTriggerInfo)
135
if (this._editingCheckBox != null)
137
bool? uneditedValue = this._editingCheckBox.IsChecked;
138
if (editingTriggerInfo != null &&
139
editingTriggerInfo.MouseButtonEventArgs != null)
141
// Editing was triggered by a mouse click
142
FrameworkElement checkBox = editingTriggerInfo.MouseButtonEventArgs.Source as FrameworkElement;
143
while (checkBox != null && !(checkBox is CheckBox))
145
checkBox = checkBox.Parent as FrameworkElement;
147
if (checkBox != null)
149
// User clicked the checkbox itself, let's toggle the IsChecked value
150
if (this._editingCheckBox.IsThreeState)
152
switch (this._editingCheckBox.IsChecked)
155
this._editingCheckBox.IsChecked = true;
158
this._editingCheckBox.IsChecked = null;
161
this._editingCheckBox.IsChecked = false;
167
this._editingCheckBox.IsChecked = !this._editingCheckBox.IsChecked;
171
return uneditedValue;
177
/// Called by the DataGrid control when this column asks for its elements to be
178
/// updated, because its CheckBoxContentBinding or IsThreeState property changed.
180
public override void UpdateElement(FrameworkElement element, string propertyName)
184
throw new ArgumentNullException("element");
186
CheckBox checkBox = element as CheckBox;
187
if (checkBox == null)
189
throw DataGridError.DataGrid.ValueIsNotAnInstanceOf("element", typeof(CheckBox));
191
if (propertyName == DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName)
193
checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding);
195
else if (propertyName == DATAGRIDCHECKBOXCOLUMN_isThreeStateName)
197
checkBox.IsThreeState = this.IsThreeState;
201
checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding);
202
checkBox.IsThreeState = this.IsThreeState;
206
#endregion Public Methods
208
#region Protected Methods
210
protected override FrameworkElement GenerateEditingElement()
212
this._editingCheckBox = new CheckBox();
214
this._editingCheckBox.Margin = new Thickness(0);
215
ConfigureCheckBox(this._editingCheckBox);
216
return this._editingCheckBox;
219
protected override FrameworkElement GenerateElement()
221
ReadOnlyCheckBox checkBoxElement = new ReadOnlyCheckBox();
222
ConfigureCheckBox(checkBoxElement);
223
return checkBoxElement;
226
#endregion Protected Methods
228
#region Private Methods
230
private void ConfigureCheckBox(CheckBox checkBox)
232
checkBox.HorizontalAlignment = HorizontalAlignment.Center;
233
checkBox.VerticalAlignment = VerticalAlignment.Center;
234
checkBox.IsThreeState = this.IsThreeState;
235
checkBox.SetBinding(CheckBox.IsCheckedProperty, this.DisplayMemberBinding);
237
if (this.CheckBoxContentBinding != null)
239
checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding);
243
private static Style InitializeCheckBoxStyle()
245
// Loads our styles for the ReadOnlyCheckBox
246
string styleXaml = null;
247
System.IO.Stream stream = typeof(DataGridCheckBoxColumn).Assembly.GetManifestResourceStream("System.Windows.Controls.DataGrid.DataGridCheckBoxColumn.xaml");
250
styleXaml = new System.IO.StreamReader(stream).ReadToEnd();
253
return XamlReader.Load(styleXaml) as Style;
256
#endregion Private Methods
260
private class ReadOnlyCheckBox : CheckBox
264
protected override void OnIndeterminate(RoutedEventArgs e)
266
DataGridCell dataGridCell = DataGrid.GetOwningCell(this);
267
if (dataGridCell != null && dataGridCell.RowIndex == -1)
271
base.OnIndeterminate(e);
274
protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e)
279
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
284
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
289
protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
294
protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
299
protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
305
#endregion Nested Types