/// <summary> /// This code builds a list of columns and their corresponding filter textboxes /// </summary> /// <param name="dataGrid"></param> /// <param name="centerCellStyle"></param> /// <param name="keyUpEvent"></param> /// <param name="txtSearchPanel"></param> /// <param name="colVM"></param> /// <param name="watermarkTextbox"></param> internal void BuildDataGrid(DataGrid dataGrid, IColumnsViewModel colVM, Style centerCellStyle, Style watermarkTextbox, KeyEventHandler keyUpEvent, Panel txtSearchPanel) { if (dataGrid == null) { return; } // Remove available columns if there are any dataGrid.Columns.Clear(); if (colVM.DataGridColumns != null) { // Create marker column and each filterable column var markerCol = CreateMarkersColumn(dataGrid, txtSearchPanel); BuildMarkerSearchPanel(txtSearchPanel, markerCol); foreach (IColumnItem item in colVM.DataGridColumns) { DataGridTextColumn col = new DataGridTextColumn(); col.Header = item.Header; // Bind column to visibility property via bool - visibility converter var visiblityBinding = new Binding("IsColumnVisible"); visiblityBinding.Source = item; visiblityBinding.Converter = _mBoolToVisConverter; BindingOperations.SetBinding(col, DataGridColumn.VisibilityProperty, visiblityBinding); if (item.Alignment == CellAlignment.CENTER && centerCellStyle != null) { col.CellStyle = centerCellStyle; } col.MinWidth = item.MinWidth; col.Width = item.Width; Binding bind = new Binding(item.Field) { Mode = BindingMode.OneWay }; bind.ConverterCulture = System.Globalization.CultureInfo.GetCultureInfo(Strings.Resources.CultureName); if (!string.IsNullOrWhiteSpace(item.StringFormat)) { bind.StringFormat = item.StringFormat; } col.Binding = bind; // Add column to datagrid dataGrid.Columns.Add(col); BuildTextSearchPanel(keyUpEvent, txtSearchPanel, col, item, watermarkTextbox); } } }
/// <summary> /// Handle the case in which the user has entered a character into any of the filter columns - /// update the corresponding filter column string in the viewmodel. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void HandleRefreshOnInputKeyInFilter(object sender, KeyEventArgs e) { var tb = sender as TextBox; if (tb == null) { return; } IColumnsViewModel colVM = Columns; if (colVM == null) { return; } IColumnItem col = colVM.DataGridColumns.FirstOrDefault(f => f.FilterControlName == tb.Name); if (col == null) { return; } col.SetColumnFilterValue(tb.Text); }
/// <summary> /// Create columns and other items (text filter textbox headers) that bring the DataGrid to live. /// </summary> private void RebuildGrid() { IColumnsViewModel colVM = Columns; Style centerCellStyle = CenterCellStyle; Style watermarkTextbox = WaterMarkTextBox; // Check the resource style for this class if any of these items is null var partDataGrid = GetTemplateChild("PART_DataGrid") as DataGrid; var partSearchPanel = GetTemplateChild("PART_SearchPanel") as Panel; if (colVM != null && partDataGrid != null) { partDataGrid.EnableRowVirtualization = true; _GridManager.BuildDataGrid(partDataGrid, colVM, centerCellStyle, watermarkTextbox, HandleRefreshOnInputKeyInFilter, partSearchPanel); ////Not necessary since the ViewModel creates the CollectionView and looks after the filtering ////this.AssignSource(new Binding("LogEntryRowViewModels") { Source = this, Mode = BindingMode.OneWay }); } }