private void self_Loaded(object sender, RoutedEventArgs e) { if (FilterHost == null) { // Find the ancestor column header and data grid controls. ColumnHeader = this.FindAncestorOrSelf <DataGridColumnHeader>(); if (ColumnHeader == null) { throw new InvalidOperationException("DataGridFilterColumnControl must be a child element of a DataGridColumnHeader."); } DataGrid = ColumnHeader.FindAncestorOrSelf <DataGrid>(); if (DataGrid == null) { throw new InvalidOperationException("DataGridColumnHeader must be a child element of a DataGrid"); } // Find our host and attach ourself. FilterHost = DataGrid.GetFilter(); } FilterHost.AddColumn(this); DataGrid.SourceUpdated += DataGrid_SourceOrTargetUpdated; DataGrid.TargetUpdated += DataGrid_SourceOrTargetUpdated; DataGrid.RowEditEnding += DataGrid_RowEditEnding; // Must set a non-null empty template here, else we won't get the coerce value callback when the columns attached property is null! Template = _emptyControlTemplate; // Bind our IsFilterVisible and Template properties to the corresponding properties attached to the // DataGridColumnHeader.Column property. Use binding instead of simple assignment since columnHeader.Column is still null at this point. var isFilterVisiblePropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.IsFilterVisibleProperty); BindingOperations.SetBinding(this, VisibilityProperty, new Binding() { Path = isFilterVisiblePropertyPath, Source = ColumnHeader, Mode = BindingMode.OneWay, Converter = _booleanToVisibilityConverter }); var templatePropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.TemplateProperty); BindingOperations.SetBinding(this, TemplateProperty, new Binding() { Path = templatePropertyPath, Source = ColumnHeader, Mode = BindingMode.OneWay }); var filterPropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.FilterProperty); BindingOperations.SetBinding(this, FilterProperty, new Binding() { Path = filterPropertyPath, Source = ColumnHeader, Mode = BindingMode.TwoWay }); }
private void DataGridFilterColumnControl_Loaded(object sender, System.Windows.RoutedEventArgs e) { if (FilterHost == null) { // Find the ancestor column header and data grid controls. ColumnHeader = this.FindColumnHeader <DataGridColumnHeader>(); if (ColumnHeader == null) { throw new InvalidOperationException("DataGridFilterColumnControl must be a child element of a DataGridColumnHeader."); } DataGrid = ColumnHeader.FindColumnHeader <DataGrid>(); if (DataGrid == null) { throw new InvalidOperationException("DataGridColumnHeader must be a child element of a DataGrid"); } // Find our host and attach ourself. FilterHost = DataGrid.GetFilter(); } Template = _emptyControlTemplate; var isFilterVisiblePropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.IsFilterVisibleProperty); BindingOperations.SetBinding(this, VisibilityProperty, new Binding() { Path = isFilterVisiblePropertyPath, Source = ColumnHeader, Mode = BindingMode.OneWay, Converter = _booleanToVisibilityConverter }); var templatePropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.TemplateProperty); BindingOperations.SetBinding(this, TemplateProperty, new Binding() { Path = templatePropertyPath, Source = ColumnHeader, Mode = BindingMode.OneWay }); var filterPropertyPath = new PropertyPath("Column.(0)", DataGridFilterColumn.FilterProperty); BindingOperations.SetBinding(this, FilterProperty, new Binding() { Path = filterPropertyPath, Source = ColumnHeader, Mode = BindingMode.TwoWay }); //1.获取datagridcolumnheader中Column是什么类型 //2.根据不同的类型修改自己的样式 //ColumnHeader = this.FindColumnHeader<DataGridColumnHeader>(); //if (ColumnHeader == null) // throw new Exception(); var template = ColumnHeader.Column.GetType(); if (template == typeof(DataGridCheckBoxColumn)) { var tem = this.FindResource("DataGridCheckBoxColumn") as ControlTemplate; var count = VisualTreeHelper.GetChildrenCount(new CheckBox()); // CheckBox c = VisualTreeHelper.GetChild(new CheckBox()) as CheckBox; // CheckBox c = tem.c("_chebox_",new Grid()) as CheckBox; // BindingOperations.SetBinding(c, CheckBox.IsCheckedProperty, new Binding(nameof(FilterValues)) { Source = this }); this.Template = tem; this.Visibility = FilterHost._isFilteringVisibility ? Visibility.Visible : Visibility.Hidden; } else { this.Template = this.FindResource("DataGridTextColumn") as ControlTemplate; this.Visibility = FilterHost._isFilteringVisibility ? Visibility.Visible : Visibility.Hidden; } FilterHost.AddColumn(this); }