~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to class/Microsoft.SilverlightControls/Controls/Data/src/DataGrid/DataGridCheckBoxColumn.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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. 
 
5
 
 
6
using System.Windows.Data;
 
7
using System.Windows.Markup;
 
8
using System.Windows.Controls;
 
9
 
 
10
namespace System.Windows.Controlsb1
 
11
 
12
    public class DataGridCheckBoxColumn : DataGridBoundColumnBase
 
13
    {
 
14
        #region Constants 
 
15
 
 
16
        private const string DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName = "CheckBoxContentBinding";
 
17
        private const string DATAGRIDCHECKBOXCOLUMN_isThreeStateName = "IsThreeState"; 
 
18
 
 
19
        #endregion Constants
 
20
 
 
21
        #region Data
 
22
 
 
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(); 
 
28
 
 
29
        #endregion Data 
 
30
 
 
31
        public DataGridCheckBoxColumn()
 
32
        { 
 
33
            this.ElementStyle = _readOnlyCheckBoxStyle;
 
34
        }
 
35
 
 
36
        #region Dependency Properties 
 
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
        #endregion Dependency Properties
 
86
 
 
87
        #region Public Properties
 
88
 
 
89
        public Binding CheckBoxContentBinding 
 
90
        { 
 
91
            get
 
92
            { 
 
93
                return this._checkBoxContentBinding;
 
94
            }
 
95
            set 
 
96
            {
 
97
                if (this._checkBoxContentBinding != value)
 
98
                { 
 
99
                    this._checkBoxContentBinding = value; 
 
100
                    UpdateElements(DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName);
 
101
                } 
 
102
            }
 
103
        }
 
104
 
 
105
        public bool IsThreeState
 
106
        {
 
107
            get 
 
108
            { 
 
109
                return this._isThreeState;
 
110
            } 
 
111
            set
 
112
            {
 
113
                if (this._isThreeState != value) 
 
114
                {
 
115
                    this._isThreeState = value;
 
116
                    UpdateElements(DATAGRIDCHECKBOXCOLUMN_isThreeStateName); 
 
117
                } 
 
118
            }
 
119
        } 
 
120
 
 
121
        #endregion Public Properties
 
122
 
 
123
        #region Public Methods
 
124
 
 
125
        public override void CancelCellEdit(object uneditedValue) 
 
126
        { 
 
127
            if (this._editingCheckBox != null)
 
128
            { 
 
129
                this._editingCheckBox.IsChecked = (bool?)uneditedValue;
 
130
            }
 
131
        } 
 
132
 
 
133
        public override object PrepareCellEdit(DataGridEditingTriggerInfo editingTriggerInfo)
 
134
        { 
 
135
            if (this._editingCheckBox != null) 
 
136
            {
 
137
                bool? uneditedValue = this._editingCheckBox.IsChecked; 
 
138
                if (editingTriggerInfo != null &&
 
139
                    editingTriggerInfo.MouseButtonEventArgs != null)
 
140
                { 
 
141
                    // Editing was triggered by a mouse click
 
142
                    FrameworkElement checkBox = editingTriggerInfo.MouseButtonEventArgs.Source as FrameworkElement;
 
143
                    while (checkBox != null && !(checkBox is CheckBox)) 
 
144
                    { 
 
145
                        checkBox = checkBox.Parent as FrameworkElement;
 
146
                    } 
 
147
                    if (checkBox != null)
 
148
                    {
 
149
                        // User clicked the checkbox itself, let's toggle the IsChecked value 
 
150
                        if (this._editingCheckBox.IsThreeState)
 
151
                        {
 
152
                            switch (this._editingCheckBox.IsChecked) 
 
153
                            { 
 
154
                                case false:
 
155
                                    this._editingCheckBox.IsChecked = true; 
 
156
                                    break;
 
157
                                case true:
 
158
                                    this._editingCheckBox.IsChecked = null; 
 
159
                                    break;
 
160
                                case null:
 
161
                                    this._editingCheckBox.IsChecked = false; 
 
162
                                    break; 
 
163
                            }
 
164
                        } 
 
165
                        else
 
166
                        {
 
167
                            this._editingCheckBox.IsChecked = !this._editingCheckBox.IsChecked; 
 
168
                        }
 
169
                    }
 
170
                } 
 
171
                return uneditedValue; 
 
172
            }
 
173
            return false; 
 
174
        }
 
175
 
 
176
        /// <summary> 
 
177
        /// Called by the DataGrid control when this column asks for its elements to be
 
178
        /// updated, because its CheckBoxContentBinding or IsThreeState property changed.
 
179
        /// </summary> 
 
180
        public override void UpdateElement(FrameworkElement element, string propertyName) 
 
181
        {
 
182
            if (element == null) 
 
183
            {
 
184
                throw new ArgumentNullException("element");
 
185
            } 
 
186
            CheckBox checkBox = element as CheckBox;
 
187
            if (checkBox == null)
 
188
            { 
 
189
                throw DataGridError.DataGrid.ValueIsNotAnInstanceOf("element", typeof(CheckBox)); 
 
190
            }
 
191
            if (propertyName == DATAGRIDCHECKBOXCOLUMN_checkBoxContentBindingName) 
 
192
            {
 
193
                checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding);
 
194
            } 
 
195
            else if (propertyName == DATAGRIDCHECKBOXCOLUMN_isThreeStateName)
 
196
            {
 
197
                checkBox.IsThreeState = this.IsThreeState; 
 
198
            } 
 
199
            else
 
200
            { 
 
201
                checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding);
 
202
                checkBox.IsThreeState = this.IsThreeState;
 
203
            } 
 
204
        }
 
205
 
 
206
        #endregion Public Methods 
 
207
 
 
208
        #region Protected Methods
 
209
 
 
210
        protected override FrameworkElement GenerateEditingElement()
 
211
        {
 
212
            this._editingCheckBox = new CheckBox(); 
 
213
            //
 
214
            this._editingCheckBox.Margin = new Thickness(0);
 
215
            ConfigureCheckBox(this._editingCheckBox); 
 
216
            return this._editingCheckBox; 
 
217
        }
 
218
 
 
219
        protected override FrameworkElement GenerateElement()
 
220
        {
 
221
            ReadOnlyCheckBox checkBoxElement = new ReadOnlyCheckBox(); 
 
222
            ConfigureCheckBox(checkBoxElement);
 
223
            return checkBoxElement;
 
224
        } 
 
225
 
 
226
        #endregion Protected Methods
 
227
 
 
228
        #region Private Methods
 
229
 
 
230
        private void ConfigureCheckBox(CheckBox checkBox) 
 
231
        {
 
232
            checkBox.HorizontalAlignment = HorizontalAlignment.Center;
 
233
            checkBox.VerticalAlignment   = VerticalAlignment.Center; 
 
234
            checkBox.IsThreeState        = this.IsThreeState; 
 
235
            checkBox.SetBinding(CheckBox.IsCheckedProperty, this.DisplayMemberBinding);
 
236
 
 
237
            if (this.CheckBoxContentBinding != null)
 
238
            {
 
239
                checkBox.SetBinding(CheckBox.ContentProperty, this.CheckBoxContentBinding); 
 
240
            }
 
241
        }
 
242
 
 
243
        private static Style InitializeCheckBoxStyle() 
 
244
        {
 
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");
 
248
            if (stream != null) 
 
249
            {
 
250
                styleXaml = new System.IO.StreamReader(stream).ReadToEnd();
 
251
                stream.Close(); 
 
252
            } 
 
253
            return XamlReader.Load(styleXaml) as Style;
 
254
        } 
 
255
 
 
256
        #endregion Private Methods
 
257
 
 
258
        #region Nested Types
 
259
 
 
260
        private class ReadOnlyCheckBox : CheckBox 
 
261
        { 
 
262
            //
 
263
 
 
264
            protected override void OnIndeterminate(RoutedEventArgs e)
 
265
            {
 
266
                DataGridCell dataGridCell = DataGrid.GetOwningCell(this); 
 
267
                if (dataGridCell != null && dataGridCell.RowIndex == -1)
 
268
                {
 
269
                    return; 
 
270
                } 
 
271
                base.OnIndeterminate(e);
 
272
            } 
 
273
 
 
274
            protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e)
 
275
            { 
 
276
                e.Handled = true;
 
277
            }
 
278
 
 
279
            protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e) 
 
280
            {
 
281
                e.Handled = true; 
 
282
            }
 
283
 
 
284
            protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e) 
 
285
            {
 
286
                e.Handled = true;
 
287
            } 
 
288
 
 
289
            protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
 
290
            { 
 
291
                e.Handled = true;
 
292
            }
 
293
 
 
294
            protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
 
295
            {
 
296
                e.Handled = false; 
 
297
            } 
 
298
 
 
299
            protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) 
 
300
            {
 
301
                e.Handled = true;
 
302
            } 
 
303
        }
 
304
 
 
305
        #endregion Nested Types 
 
306
    } 
 
307
}