項目中用到DataGrid, 需要在第一列添加checkbox, 可以多選、全選。
其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellControlTemplate,Binding, OnPropertyChanged等。
有下面是實現思路:
1.繼承INotifyPropertyChanged接口,實現OnPropertyChanged方法:
public abstract class ViewModelBase : INotifyPropertyChanged
? {
? ? public event PropertyChangedEventHandler PropertyChanged;
? ? ? /// <summary>
? ? ? /// Raises this object's PropertyChanged event.
? ? ? /// </summary>
? ? ? /// <param name="propertyName">The property that has a new value</param>
? ? ? protected void OnPropertyChanged(string propertyName)
? ? ? {
? ? ? ? ? PropertyChangedEventHandler handler = PropertyChanged;
? ? ? ? ? if (null != handler)
? ? ? ? ? {
? ? ? ? ? ? ? handler(this, new PropertyChangedEventArgs(propertyName));
? ? ? ? ? }
? ? ? }
//..................
}
2. 實現viewModel, 添加IsSelected屬性, 存儲當前多選狀態
?? ? ??private bool _isSelected = true;
? ? ? public bool IsSelected
? ? ? {
? ? ? ? ? get { return _isSelected; }
? ? ? ? ? set
? ? ? ? ? {
? ? ? ? ? ? ? _isSelected = value;
? ? ? ? ? ? ? OnPropertyChanged("IsSelected");
? ? ? ? ? }
? ? ? }
3.在VM/Model準備好后, 我們接下來開始對DataGrid進行style自定義
4.準備checkbox的DataTemplate:
<DataTemplate x:Key="CheckboxDataTemplate1">
? ? ? ? <Grid>
? ? ? ? <CheckBox x:Name="_chkSelected"
? ? ? ? ? ? ? ? ? ? ? Height="16"
? ? ? ? ? ? ? ? ? ? ? HorizontalAlignment="Center"
? ? ? ? ? ? ? ? ? ? ? VerticalAlignment="Center"
? ? ? ? ? ? ? ? ? ? ? Background="{x:Null}"
? ? ? ? ? ? ? ? ? ? ? VerticalContentAlignment="Center"
? ? ? ? ? ? ? ? ? ? ? HorizontalContentAlignment="Center"
? ? ? ? ? ? ? ? ? ? ? Click="_chkSelected_OnClick"
? ? ? ? ? ? ? ? ? ? ? IsThreeState="False"
? ? ? ? ? ? ? ? ? ? ? IsChecked="{Binding IsSelected, Mode=OneWay, FallbackValue=True}"
? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? </Grid>
? ? ? </DataTemplate>
此示例中checkbox只有簡單的2種狀態,
1)IsChecked屬性綁定VM的IsSelected屬性;
2)Mode為OneWay是因為我們需求是用戶可以多選行然后點擊某行頭選中多行。此功能在Click事件中遍歷當前所有選中的行,然后更改其VM的IsSelected屬性。因此不需要TwoWay模式;
3)在Binding中添加了FallbackValue, 此屬性指示當binding失敗時給出的默認值。此例中因為DataTemplate也應用在列頭, 而列頭的DataContext和DataGridRow不同;
4)在Click事件處理函數中,判斷當前點擊的列頭還是行頭, 更改對應DataContext的IsSelected屬性。 如:
? ??private void _chkSelected_OnClick(object sender, RoutedEventArgs e)
? ? ? {
? ? ? ? ? CheckBox chkSelected = e.OriginalSource as CheckBox;
? ? ? ? ? if (null == chkSelected)
? ? ? ? ? {
? ? ? ? ? ? ? return;
? ? ? ? ? }
? ? ? ? ? var studyModel = chkSelected.DataContext as StudyModel;
? ? ? ? ? bool isChecked = chkSelected.IsChecked.HasValue ? chkSelected.IsChecked.Value : true;
? ? ? ? ? FrameworkElement templateParent = chkSelected.TemplatedParent is FrameworkElement
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (chkSelected.TemplatedParent as FrameworkElement).TemplatedParent as FrameworkElement
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? : null;
? ? ? ? ? if (templateParent is DataGridColumnHeader)
? ? ? ? ? {
? ? ? ? ? ? ? MainViewModel mvm = this.DataContext as MainViewModel;
? ? ? ? ? ? ? if (null != mvm)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? foreach (var sm in mvm.StudyList)
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? sm.IsSelected = isChecked;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? else if (templateParent is DataGridCell)
? ? ? ? ? {
? ? ? ? ? ? ? if (null != studyModel && null != this._grdStudyList.SelectedItems && this._grdStudyList.SelectedItems.Contains(studyModel))
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? foreach (var otherSelected in this._grdStudyList.SelectedItems.OfType<StudyModel>())
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? otherSelected.IsSelected = isChecked;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ?其中MainViewModel為主VM, 其包含一個ObservableCollection<StudyModel>?StudyList, 而StudyModel包含IsSelected屬性, 二者都實現OnpropertyChanged方法; _grdStudyList為xaml中的DataGrid
5.應用DataTemplate到DataGridColumnHeader和DataGridCell, 如:
? ? <Style x:Key="DataGridCheckboxColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
? ? ? ? ? <Setter Property="ContentTemplate" Value="{DynamicResource CheckboxDataTemplate1}"/>
? ? ? ? ? <Setter Property="HorizontalAlignment" Value="Stretch"/>
? ?? ? ?? <Setter Property="VerticalAlignment" Value="Stretch"/>
? ??</Style>
? ??<Style x:Key="DataGridCheckboxCellStyle1" TargetType="{x:Type DataGridCell}">? ? ??
? ? ? <Setter Property="Padding" Value="20,0"/>? ? ? ? ? ? ? ??
?? ? ??<Setter Property="ContentTemplate" Value="{DynamicResource CheckboxDataTemplate1}"/> ? ??
? ? ? <Setter Property="Background" Value="#FFC1C1C1"/>? ? ? ??
? ? ? <Setter Property="BorderBrush" Value="{x:Null}"/>? ? ? ? ? ? ??
?? ? ??<Setter Property="BorderThickness" Value="0"/> ? ? ? ? ? ? ??
?? ? ?<Setter Property="Template" Value="{DynamicResource DataGridCheckboxCellControlTemplate1}"/> ? ? ?
? ? ? <Style.Triggers>? ? ? ? ? ? ? ? ? ? ? ? ?
? ???? ? ??<Trigger Property="IsSelected" Value="True"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
??? ? ?? ? ?? ? ??<Setter Property="Background" Value="#FFC1C1C1"/> ? ? ? ? ? ? ? ? ? ? ??
? ??? ? ??? ? ???<Setter Property="BorderBrush" Value="{x:Null}"/> ? ? ? ? ? ? ? ? ?
? ? ??? ? ??</Trigger> ? ? ? ? ??
? ?? ?</Style.Triggers> ? ? ? ? ??
? ??</Style> ? ? ? ? ? ? ? ? ? ?
?? ? ?<ControlTemplate x:Key="DataGridCheckboxCellControlTemplate1" TargetType="{x:Type DataGridCell}"> ? ? ? ?
? ? ? ? <Border
? ? ? ? ? ? ? ? BorderBrush="{TemplateBinding BorderBrush}"
? ? ? ? ? ? ? ? BorderThickness="{TemplateBinding BorderThickness}"
? ? ? ? ? ? ? ? Background="{TemplateBinding Background}"
? ? ? ? ? ? ? ? SnapsToDevicePixels="True">? ? ? ? ? ? ? ? ? ??
? ?? ? ?? ? ? <ContentPresenter?
? ? ? ? ? ? ? ? ? ? ContentTemplate="{TemplateBinding ContentTemplate}"
? ? ? ? ? ? ? ? ? ? Content="{TemplateBinding Content}"
? ? ? ? ? ? ? ? ? ? ContentStringFormat="{TemplateBinding ContentStringFormat}"
? ? ? ? ? ? ? ? ? ? SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>? ? ? ? ? ? ? ??
?? ? ?? ?</Border>
??? ?</ControlTemplate>
6.應用CellStyle和HeaderStyle到DataGrid:
? ?<DataGrid
~~~
x:Name="_grdStudyList"
ItemsSource="{Binding StudyList}"
AutoGenerateColumns="False"
FrozenColumnCount="1" Background="#FF999797">
~~~
~~~
<DataGrid.Columns>
~~~
~~~
<DataGridCheckBoxColumn
~~~
~~~
x:Name="_dtcSelected"
Header=""
HeaderStyle="{StaticResource DataGridCheckboxColumnHeaderStyle1}"
CellStyle="{StaticResource DataGridCheckboxCellStyle1}"
MinWidth="60" CanUserReorder="False" MaxWidth="60"/>
~~~
- 前言
- win32與WPF的混合編程
- WPF: 一個可以用StoryBoard動態改變Grid行寬/列高的類
- MFC中調用WPF教程
- Expression Blend操作: 使用behavior來控制Storyboard
- WPF DatePicker 的textbox的焦點
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事項
- WPF TreeView 后臺C#選中指定的Item, 需要遍歷
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后臺觸發 Validate UI‘s Element
- WPF ValidationRule 觸發ErrorTemplate 的注意事項
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 顯示選中的item
- 如何得到WPF中控件綁定的EventTrigger
- 選中DataGrid的Cell而不是row
- ContextMenu的自定義
- 輸入框只能輸入英文
- TextBox的OnTextboxChanged事件里對Text重新賦值帶中文, 導致崩潰
- DataGrid當列寬超出當前寬度時,沒有數據也恒有滾動條
- wpf如何獲取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible設為Collapse時,VisualTreeHelper.GetChildrenCount為0
- XAML 編碼規范 (思考)
- 如何為現有控件的DependencyProperty添加Value Changed事件?
- TreeView滾動TreeViewItem
- 為BindingList添加Sort
- WPF Background的設置有坑
- 自定義Panel中添加依賴屬性需要注意的問題
- TextBlock截斷字符顯示為....
- DataGrid 支持字符截斷顯示
- TreeView控件實踐
- WPF如何更改系統控件的默認高亮顏色 (Highlight brush)
- ViewModel中C# Property自動添加OnPropertyChanged處理的小工具, 以及相應Python知識點
- WPF中Xaml編譯正常而Designer Time時出錯的解決辦法
- 關于Snoop的用法
- wpf中為DataGrid添加checkbox支持多選全選
- WPF中DataGrid控件的過濾(Filter)性能分析及優化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect導致下拉框控件抖動
- 再論WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何設計歷史記錄查看UI
- WPF案例:如何設計搜索框(自定義控件的原則和方法)
- WPF基本概念入門
- WPF開發中Designer和碼農之間的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字體的設置
- Bug:DataGridCell的顯示不完整
- WPF中ToolTip的自定義
- WPF中ItemsControl綁定到Google ProtocolBuffer的結構體時的性能問題
- TreeView的性能問題
- Xaml中string(字符串)常量的定義以及空格的處理
- 依賴屬性
- WPF中的CheckBox的_ (underscore / 下劃線)丟失
- WPF錯誤:必須使“Property”具有非 null 值。
- WPF中ItemsControl應用虛擬化時找到子元素的方法
- WPF毫秒級桌面時鐘的實現-C#中Hook(鉤子)的應用
- KB2464222導致IsNonIdempotentProperty方法找不見
- WPF中PreviewMouseDownEvent的系統處理:TabItem的PreviewMouseDown 事件彈框后不切換的問題調查
- WPF文字渲染相關的問題及解決
- wpf中的默認右鍵菜單中的復制、粘貼、剪貼等沒有本地化的解決方案
- WPF內部DeliverEvent讀鎖和PrivateAddListener寫鎖導致死鎖
- Windbg調試WPF的依賴屬性
- WPF 后臺Render線程崩潰, Exception from HRESULT: 0x88980406
- WPF中DependencyObject與DependencyProperty的源碼簡單剖析
- 禁用WPF中DataGrid默認的鼠標左鍵拖動多選行的效果
- wpf工程中在Xaml文件下添加多個cs文件
- ScrollViewer滾動到底來觸發加載數據的Behavior