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

« back to all changes in this revision

Viewing changes to class/Microsoft.SilverlightControls/Controls/Data/src/DataGrid/DataGridColumnBase.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.Diagnostics;
 
7
 
 
8
namespace System.Windows.Controlsb1
 
9
{
 
10
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes", Justification="IComparable in Jolt has only one method, probably a false positive in FxCop.")] 
 
11
    public abstract class DataGridColumnBase : /* DependencyObject,*/ IComparable<DataGridColumnBase>
 
12
    {
 
13
        #region Constants 
 
14
 
 
15
        private const byte DATAGRIDCOLUMN_defaultMinWidth = 0;
 
16
        internal const int  DATAGRIDCOLUMN_maximumWidth = 65536; 
 
17
        internal const byte DATAGRIDCOLUMN_minMinWidth = 2; 
 
18
 
 
19
        #endregion Constants 
 
20
 
 
21
        #region Data
 
22
 
 
23
        private Style _cellStyle; //
 
24
        private int _displayIndex;
 
25
        private object _header; 
 
26
        private DataGridColumnHeader _headerCell; 
 
27
        private Style _headerStyle; //
 
28
        private bool _isFrozen; 
 
29
        private bool _isReadOnly;
 
30
        private Visibility _visibility;
 
31
 
 
32
        /*
 
33
 
 
34
*/ 
 
35
 
 
36
        #endregion Data
 
37
 
 
38
        protected DataGridColumnBase()
 
39
        {
 
40
            this._visibility = Visibility.Visible; 
 
41
            this.MinWidth = DATAGRIDCOLUMN_defaultMinWidth;
 
42
            this._displayIndex = -1;
 
43
        } 
 
44
 
 
45
        #region IComparable<DataGridColumnBase> Implementation
 
46
 
 
47
        public int CompareTo(DataGridColumnBase other)
 
48
        {
 
49
            return this._displayIndex - other._displayIndex; 
 
50
        }
 
51
 
 
52
        #endregion IComparable<DataGridColumnBase> Implementation 
 
53
 
 
54
        #region Dependency Properties
 
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
 
 
178
 
 
179
 
 
180
 
 
181
 
 
182
 
 
183
 
 
184
 
 
185
 
 
186
 
 
187
 
 
188
 
 
189
 
 
190
 
 
191
 
 
192
 
 
193
 
 
194
 
 
195
 
 
196
 
 
197
 
 
198
 
 
199
 
 
200
 
 
201
 
 
202
 
 
203
 
 
204
 
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210
 
 
211
 
 
212
 
 
213
 
 
214
 
 
215
 
 
216
 
 
217
 
 
218
 
 
219
 
 
220
 
 
221
 
 
222
 
 
223
 
 
224
 
 
225
 
 
226
 
 
227
 
 
228
 
 
229
 
 
230
 
 
231
 
 
232
 
 
233
 
 
234
 
 
235
 
 
236
 
 
237
 
 
238
 
 
239
 
 
240
 
 
241
 
 
242
 
 
243
 
 
244
 
 
245
 
 
246
 
 
247
 
 
248
 
 
249
 
 
250
 
 
251
 
 
252
 
 
253
 
 
254
 
 
255
 
 
256
 
 
257
 
 
258
 
 
259
 
 
260
 
 
261
 
 
262
 
 
263
 
 
264
 
 
265
 
 
266
 
 
267
 
 
268
 
 
269
 
 
270
 
 
271
 
 
272
 
 
273
 
 
274
 
 
275
 
 
276
 
 
277
 
 
278
 
 
279
 
 
280
 
 
281
 
 
282
 
 
283
 
 
284
 
 
285
 
 
286
 
 
287
 
 
288
 
 
289
 
 
290
 
 
291
 
 
292
 
 
293
 
 
294
 
 
295
 
 
296
 
 
297
 
 
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
 
 
304
 
 
305
 
 
306
 
 
307
 
 
308
 
 
309
 
 
310
 
 
311
 
 
312
 
 
313
 
 
314
 
 
315
 
 
316
 
 
317
 
 
318
 
 
319
 
 
320
 
 
321
 
 
322
 
 
323
 
 
324
 
 
325
 
 
326
 
 
327
 
 
328
 
 
329
 
 
330
 
 
331
 
 
332
 
 
333
 
 
334
 
 
335
 
 
336
 
 
337
 
 
338
 
 
339
 
 
340
 
 
341
 
 
342
 
 
343
 
 
344
 
 
345
 
 
346
 
 
347
 
 
348
 
 
349
 
 
350
 
 
351
 
 
352
 
 
353
 
 
354
 
 
355
 
 
356
 
 
357
 
 
358
 
 
359
*/ 
 
360
 
 
361
        #endregion 
 
362
 
 
363
        #region Public Properties
 
364
 
 
365
        //
 
366
        public Style CellStyle
 
367
        { 
 
368
            get 
 
369
            {
 
370
                return _cellStyle; 
 
371
            }
 
372
            set
 
373
            { 
 
374
                if (_cellStyle != value)
 
375
                {
 
376
                    _cellStyle = value; 
 
377
                    if (this.OwningGrid != null) 
 
378
                    {
 
379
                        this.OwningGrid.OnColumnCellStyleChanged(this); 
 
380
                    }
 
381
                }
 
382
            } 
 
383
        }
 
384
 
 
385
        public bool? CanUserResize 
 
386
        { 
 
387
            get;
 
388
            set; 
 
389
        }
 
390
 
 
391
        // 
 
392
        public Style HeaderStyle
 
393
        {
 
394
            get 
 
395
            { 
 
396
                return _headerStyle;
 
397
            } 
 
398
            set
 
399
            {
 
400
                if (_headerStyle != value) 
 
401
                {
 
402
                    _headerStyle = value;
 
403
                    this.HeaderCell.Style = value; 
 
404
                } 
 
405
            }
 
406
        } 
 
407
 
 
408
        public int DisplayIndex
 
409
        { 
 
410
            get
 
411
            {
 
412
                return this._displayIndex; 
 
413
            } 
 
414
            set
 
415
            { 
 
416
                if (this._displayIndex != value)
 
417
                {
 
418
                    if (value == Int32.MaxValue) 
 
419
                    {
 
420
                        throw DataGridError.DataGrid.ValueMustBeLessThan("value", "DisplayIndex", Int32.MaxValue);
 
421
                    } 
 
422
                    if (this.OwningGrid != null) 
 
423
                    {
 
424
                        if (value < 0 || value >= this.OwningGrid.Columns.Count) 
 
425
                        {
 
426
                            throw DataGridError.DataGrid.ValueMustBeBetween("value", "DisplayIndex", 0, true, this.OwningGrid.Columns.Count, false);
 
427
                        } 
 
428
                        // Will throw an error if a visible frozen column is placed inside a non-frozen area or vice-versa.
 
429
                        this.OwningGrid.OnColumnDisplayIndexChanging(this, value);
 
430
                        this._displayIndex = value; 
 
431
                        try 
 
432
                        {
 
433
                            this.OwningGrid.InDisplayIndexAdjustments = true; 
 
434
                            this.OwningGrid.OnColumnDisplayIndexChanged_PreNotification();
 
435
                            this.OwningGrid.OnColumnDisplayIndexChanged(this);
 
436
                            this.OwningGrid.OnColumnDisplayIndexChanged_PostNotification(); 
 
437
                        }
 
438
                        finally
 
439
                        { 
 
440
                            this.OwningGrid.InDisplayIndexAdjustments = false; 
 
441
                        }
 
442
                    } 
 
443
                    else
 
444
                    {
 
445
                        if (value < -1) 
 
446
                        {
 
447
                            throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "DisplayIndex", -1);
 
448
                        } 
 
449
                        this._displayIndex = value; 
 
450
                    }
 
451
                } 
 
452
            }
 
453
        }
 
454
 
 
455
        public object Header
 
456
        {
 
457
            get 
 
458
            { 
 
459
                return this._header;
 
460
            } 
 
461
            set
 
462
            {
 
463
                if (this._header != value) 
 
464
                {
 
465
                    this._header = value;
 
466
                    if (this._headerCell != null) 
 
467
                    { 
 
468
                        this._headerCell.Content = this._header;
 
469
                    } 
 
470
                }
 
471
            }
 
472
        } 
 
473
 
 
474
        public int Index
 
475
        { 
 
476
            get; 
 
477
            internal set;
 
478
        } 
 
479
 
 
480
        public bool IsReadOnly
 
481
        { 
 
482
            get
 
483
            {
 
484
                if (this.OwningGrid == null) 
 
485
                { 
 
486
                    return this._isReadOnly;
 
487
                } 
 
488
                return this.OwningGrid.GetColumnReadOnlyState(this, this._isReadOnly);
 
489
            }
 
490
            set 
 
491
            {
 
492
                if (value != this.IsReadOnly)
 
493
                { 
 
494
                    if (this.OwningGrid != null) 
 
495
                    {
 
496
                        this.OwningGrid.OnColumnReadOnlyStateChanging(this, value); 
 
497
                    }
 
498
                    this._isReadOnly = value;
 
499
                    // 
 
500
 
 
501
 
 
502
 
 
503
 
 
504
                }
 
505
            } 
 
506
        }
 
507
 
 
508
        // 
 
509
        public double MinWidth
 
510
        {
 
511
            get 
 
512
            { 
 
513
                return this.InheritedMinWidth;
 
514
            } 
 
515
            set
 
516
            {
 
517
                if (this.LocalMinWidth != value) 
 
518
                {
 
519
                    if (value < DATAGRIDCOLUMN_minMinWidth && value != 0)
 
520
                    { 
 
521
                        throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "MinWidth", DATAGRIDCOLUMN_minMinWidth); 
 
522
                    }
 
523
                    double newEffectiveMinWidth; 
 
524
                    if (value == 0)
 
525
                    {
 
526
                        if (this.OwningGrid == null) 
 
527
                        {
 
528
                            newEffectiveMinWidth = DataGrid.DATAGRID_defaultMinColumnWidth;
 
529
                        } 
 
530
                        else 
 
531
                        {
 
532
                            newEffectiveMinWidth = this.OwningGrid.MinColumnWidth; 
 
533
                        }
 
534
                    }
 
535
                    else 
 
536
                    {
 
537
                        newEffectiveMinWidth = value;
 
538
                    } 
 
539
                    if (this.Width < newEffectiveMinWidth) 
 
540
                    {
 
541
                        this.Width = newEffectiveMinWidth; 
 
542
                    }
 
543
                    this.LocalMinWidth = value;
 
544
                    // 
 
545
 
 
546
 
 
547
 
 
548
                } 
 
549
            }
 
550
        } 
 
551
 
 
552
        //
 
553
        public Visibility Visibility 
 
554
        {
 
555
            get
 
556
            { 
 
557
                return this._visibility; 
 
558
            }
 
559
            set 
 
560
            {
 
561
                if (value != this.Visibility)
 
562
                { 
 
563
                    if (this.OwningGrid != null)
 
564
                    {
 
565
                        this.OwningGrid.OnColumnVisibleStateChanging(this); 
 
566
                    } 
 
567
                    this._visibility = value;
 
568
                    if (this.OwningGrid != null) 
 
569
                    {
 
570
                        this.OwningGrid.OnColumnVisibleStateChanged(this);
 
571
                    } 
 
572
                }
 
573
            }
 
574
        } 
 
575
 
 
576
        public double Width
 
577
        { 
 
578
            get
 
579
            {
 
580
                return this.InheritedWidth; 
 
581
            }
 
582
            set
 
583
            { 
 
584
                double minWidth = this.InheritedMinWidth; 
 
585
                if (value < minWidth)
 
586
                { 
 
587
                    // If it's 0, we inherit the width from the DataGrid; We don't need to check
 
588
                    // the inherited value here because it's already checked when it's set
 
589
                    if (value != 0) 
 
590
                    {
 
591
                        throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "Width", minWidth);
 
592
                    } 
 
593
                } 
 
594
                if (value > DATAGRIDCOLUMN_maximumWidth)
 
595
                { 
 
596
                    throw DataGridError.DataGrid.ValueMustBeLessThanOrEqualTo("value", "Width", DATAGRIDCOLUMN_maximumWidth);
 
597
                }
 
598
                double oldInheritedWidth = this.InheritedWidth; 
 
599
                double newInheritedWidth;
 
600
                if (value == 0)
 
601
                { 
 
602
                    newInheritedWidth = (this.OwningGrid == null) ? DataGrid.DATAGRID_defaultColumnWidth : this.OwningGrid.ColumnWidth; 
 
603
                }
 
604
                else 
 
605
                {
 
606
                    newInheritedWidth = value;
 
607
                } 
 
608
                if (oldInheritedWidth != newInheritedWidth)
 
609
                {
 
610
                    // 
 
611
 
 
612
 
 
613
 
 
614
                    this.LocalWidth = value;
 
615
                    if (this.OwningGrid != null)
 
616
                    { 
 
617
                        this.OwningGrid.OnColumnWidthChanged(this);
 
618
                    }
 
619
                } 
 
620
            } 
 
621
        }
 
622
 
 
623
        #endregion Public Properties
 
624
 
 
625
        #region Internal Properties 
 
626
 
 
627
        internal bool DisplayIndexHasChanged
 
628
        { 
 
629
            get; 
 
630
            set;
 
631
        } 
 
632
 
 
633
        internal int DisplayIndexInternal
 
634
        { 
 
635
            set
 
636
            {
 
637
                Debug.Assert(value >= -1); 
 
638
                Debug.Assert(value < Int32.MaxValue); 
 
639
 
 
640
                this._displayIndex = value; 
 
641
            }
 
642
        }
 
643
 
 
644
        internal bool HasHeaderCell
 
645
        {
 
646
            get 
 
647
            { 
 
648
                return this._headerCell != null;
 
649
            } 
 
650
        }
 
651
 
 
652
        /* 
 
653
 
 
654
 
 
655
 
 
656
 
 
657
 
 
658
 
 
659
 
 
660
 
 
661
 
 
662
 
 
663
 
 
664
 
 
665
 
 
666
 
 
667
*/ 
 
668
 
 
669
        internal DataGridColumnHeader HeaderCell
 
670
        { 
 
671
            get
 
672
            {
 
673
                if (this._headerCell == null) 
 
674
                { 
 
675
                    this._headerCell = new DataGridColumnHeader();
 
676
 
 
677
                    // Take the style set on this column or the one defined at the DataGrid level
 
678
                    Style style = this.HeaderStyle;
 
679
                    if (style == null && this.OwningGrid != null) 
 
680
                    {
 
681
                        style = this.OwningGrid.ColumnHeaderStyle;
 
682
                    } 
 
683
                    if (style != null) 
 
684
                    {
 
685
                        this._headerCell.Style = style; 
 
686
                    }
 
687
 
 
688
                    this._headerCell.OwningColumn = this; 
 
689
                    this._headerCell.Content = this._header;
 
690
                }
 
691
                return this._headerCell; 
 
692
            } 
 
693
        }
 
694
 
 
695
        internal double InheritedMinWidth
 
696
        {
 
697
            get 
 
698
            {
 
699
                if (this.LocalMinWidth == 0)
 
700
                { 
 
701
                    return (this.OwningGrid == null) ? DataGrid.DATAGRID_defaultMinColumnWidth : this.OwningGrid.MinColumnWidth; 
 
702
                }
 
703
                return this.LocalMinWidth; 
 
704
            }
 
705
        }
 
706
 
 
707
        internal double InheritedWidth
 
708
        {
 
709
            get 
 
710
            { 
 
711
                if (this.LocalWidth == 0)
 
712
                { 
 
713
                    return this.OwningGrid == null ? DataGrid.DATAGRID_defaultColumnWidth : this.OwningGrid.ColumnWidth;
 
714
                }
 
715
                return this.LocalWidth; 
 
716
            }
 
717
        }
 
718
 
 
719
        // 
 
720
        internal bool IsFrozen
 
721
        { 
 
722
            get
 
723
            {
 
724
                return this._isFrozen; 
 
725
            }
 
726
            set
 
727
            { 
 
728
                if (value != this.IsFrozen) 
 
729
                {
 
730
                    if (this.OwningGrid != null) 
 
731
                    {
 
732
                        this.OwningGrid.OnColumnFrozenStateChanging(this);
 
733
                    } 
 
734
                    this._isFrozen = value;
 
735
                    if (this.OwningGrid != null)
 
736
                    { 
 
737
                        this.OwningGrid.OnColumnFrozenStateChanged(this); 
 
738
                    }
 
739
                } 
 
740
            }
 
741
        }
 
742
 
 
743
        /*
 
744
 
 
745
 
 
746
 
 
747
 
 
748
 
 
749
 
 
750
 
 
751
 
 
752
 
 
753
 
 
754
 
 
755
*/ 
 
756
 
 
757
        internal double LocalMinWidth 
 
758
        {
 
759
            get;
 
760
            private set; 
 
761
        }
 
762
 
 
763
        internal double LocalWidth 
 
764
        { 
 
765
            get;
 
766
            private set; 
 
767
        }
 
768
 
 
769
        internal DataGrid OwningGrid 
 
770
        {
 
771
            get;
 
772
            set; 
 
773
        } 
 
774
 
 
775
        /* 
 
776
 
 
777
 
 
778
 
 
779
 
 
780
 
 
781
 
 
782
 
 
783
 
 
784
 
 
785
 
 
786
 
 
787
 
 
788
 
 
789
 
 
790
 
 
791
*/ 
 
792
 
 
793
        internal Type Type 
 
794
        {
 
795
            get;
 
796
            set; 
 
797
        }
 
798
 
 
799
        #endregion Internal Properties 
 
800
 
 
801
        #region Public Methods
 
802
 
 
803
        public FrameworkElement GetElement(DataGridRow dataGridRow)
 
804
        {
 
805
            if (dataGridRow == null) 
 
806
            {
 
807
                throw new ArgumentNullException("dataGridRow");
 
808
            } 
 
809
            if (this.OwningGrid == null) 
 
810
            {
 
811
                throw DataGridError.DataGrid.NoOwningGrid(this.GetType()); 
 
812
            }
 
813
            Debug.Assert(this.Index >= 0);
 
814
            Debug.Assert(this.Index < this.OwningGrid.Columns.Count); 
 
815
            DataGridCell dataGridCell = dataGridRow.Cells[this.Index];
 
816
            Debug.Assert(dataGridCell != null);
 
817
            return dataGridCell.Content as FrameworkElement; 
 
818
        } 
 
819
 
 
820
        public FrameworkElement GetElement(object dataItem) 
 
821
        {
 
822
            if (dataItem == null)
 
823
            { 
 
824
                throw new ArgumentNullException("dataItem");
 
825
            }
 
826
            if (this.OwningGrid == null) 
 
827
            { 
 
828
                throw DataGridError.DataGrid.NoOwningGrid(this.GetType());
 
829
            } 
 
830
            Debug.Assert(this.Index >= 0);
 
831
            Debug.Assert(this.Index < this.OwningGrid.Columns.Count);
 
832
            DataGridRow dataGridRow = this.OwningGrid.GetRowFromItem(dataItem); 
 
833
            if (dataGridRow == null)
 
834
            {
 
835
                // 
 
836
                return null; 
 
837
            }
 
838
            return GetElement(dataGridRow); 
 
839
        }
 
840
 
 
841
        /// <summary> 
 
842
        /// Called by the DataGrid control when a column asked for its
 
843
        /// elements to be updated, typically because one of its properties changed.
 
844
        /// </summary> 
 
845
        /// <param name="element">Indicates the element that needs to be updated</param> 
 
846
        /// <param name="propertyName">Indicates which property changed and caused this call</param>
 
847
        public virtual void UpdateElement(FrameworkElement element, string propertyName) 
 
848
        {
 
849
        }
 
850
 
 
851
        #endregion Public Methods
 
852
 
 
853
        #region Protected Methods 
 
854
 
 
855
        /// <summary>
 
856
        /// Called by a specific column type when one of its properties changed, 
 
857
        /// and its current cells need to be updated.
 
858
        /// </summary>
 
859
        /// <param name="propertyName">Indicates which property changed and caused this call</param> 
 
860
        protected void UpdateElements(string propertyName)
 
861
        {
 
862
            if (this.OwningGrid == null) 
 
863
            { 
 
864
                return;
 
865
            } 
 
866
            this.OwningGrid.UpdateColumnElements(this, propertyName);
 
867
        }
 
868
 
 
869
        #endregion Protected Methods
 
870
 
 
871
        #region Internal Methods 
 
872
 
 
873
        /*
 
874
 
 
875
 
 
876
 
 
877
 
 
878
*/
 
879
 
 
880
        #endregion Internal Methods 
 
881
 
 
882
        #region Private Methods
 
883
 
 
884
        /*
 
885
 
 
886
 
 
887
 
 
888
 
 
889
 
 
890
 
 
891
 
 
892
 
 
893
 
 
894
 
 
895
 
 
896
*/
 
897
 
 
898
        #endregion Private Methods 
 
899
 
 
900
    }
 
901