Home Forums WPF controls Xceed DataGrid for WPF Fixed Header Row

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25217 |

    Hi,
    I have inherited DataRow in order to have a filter row, and placed this type of row in the FixedHeader section. So far so good, but… When the data source gets populated the first row added to the grid has the same type as the row in the fixed header. Could you please advise how i can fix this..or why is it happening?

    Thanks.

    Imported from legacy forums. Posted by abatti (had 2464 views)

    Xceed Support
    Member
    Post count: 5658

    Can you share your code ( XAML and CS )?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 274 views)

    User (Old forums)
    Member
    Post count: 23064

    <b>
    Hi Marcus,
    Firs i have DataRowFilter like this:
    </b>
    <i>
    public class DataRowFilter : DataRow
    {
    public DataRowFilter()
    { }

    protected override Cell CreateCell(Column column)
    {
    return new DataCellFilter();
    }
    }
    </i>
    <b>
    Then i have inherited DataCell applying my own template for displaying always a combobox, and stop the mouse entered base class method getting invoked to stop showing the default Column.CellEditor. At the moement i have added the hack to check if there is a DataContext for the parent row then reapply the original control template.
    </b>
    <i>
    public partial class DataCellFilter : DataCell
    {
    #region Properties

    private ControlTemplate _originalControlTemplate;

    private ComboBox _cmbFilter;

    #endregion

    public DataCellFilter()
    {

    InitializeComponent();
    _originalControlTemplate = base.Template;
    this.Template = this.Resources[“cellFilter”] as ControlTemplate;

    this.Loaded += new RoutedEventHandler(OnLoaded);
    }

    protected override void Initialize(DataGridControl gridControl, Row parentRow, Column parentColumn)
    {
    base.Initialize(gridControl, parentRow, parentColumn);

    gridControl.DataContextChanged += new DependencyPropertyChangedEventHandler(OnDataContextChanged);
    this.ParentRow.Loaded += new RoutedEventHandler(OnParentRowLoaded);
    }

    void OnParentRowLoaded(object sender, RoutedEventArgs e)
    {
    if (null != this.ParentRow.DataContext)
    {
    this.Template = _originalControlTemplate;
    }
    }

    void OnLoaded(object sender, RoutedEventArgs e)
    {

    object oCombo = this.Template.FindName(“cmbFilter”, this);
    if (null != oCombo)
    {
    _cmbFilter = oCombo as ComboBox;
    _cmbFilter.Items.Add(“All”);
    _cmbFilter.SelectedIndex = 0;

    _cmbFilter.DropDownOpened += new EventHandler(OnFilterOpened);
    _cmbFilter.SelectionChanged += new SelectionChangedEventHandler(OnFilterSelectionChanged);
    }
    }

    void OnFilterOpened(object sender, EventArgs e)
    {
    if (_cmbFilter.Items.Count > 1)
    {
    return;
    }

    ClearFilterItems();

    DataGridControl grid = DataGridControl.GetParentDataGridControl(this);

    ItemCollection collection = grid.Items;
    if (collection != null && collection.Count > 0)
    {
    object item = collection[0];

    PropertyInfo pi = item.GetType().GetProperty(this.ParentColumn.FieldName);

    Dictionary<object, bool> filtersHash = new Dictionary<object, bool>();

    object filter = Convert.ToString(pi.GetValue(item, null));
    filtersHash.Add(filter,true);
    _cmbFilter.Items.Add(filter);

    for (int i = 0; i < collection.Count; ++i)
    {
    item = collection[i];
    filter = pi.GetValue(item, null);

    if (!filtersHash.ContainsKey(filter))
    {
    filtersHash.Add(filter, true);
    _cmbFilter.Items.Add(filter);
    }
    }
    }

    void OnFilterSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    //apply the filter on the grid
    DataGridControl grid = DataGridControl.GetParentDataGridControl(this);
    if (_cmbFilter.SelectedIndex == 0)
    {
    grid.Items.Filter = null;
    }
    else
    {
    grid.Items.Filter = new Predicate<object>(delegate (object target)
    {
    PropertyInfo pi = target.GetType().GetProperty(this.ParentColumn.FieldName);
    object value = pi.GetValue(target, null);
    return _cmbFilter.Items[_cmbFilter.SelectedIndex].Equals(value);
    });
    }
    }

    protected override void OnMouseEnter(MouseEventArgs e)
    {
    if (this.ParentRow.DataContext != null)
    {
    base.OnMouseEnter(e);
    }
    }

    private void ClearFilterItems()
    {
    while (_cmbFilter.Items.Count > 1)
    {
    _cmbFilter.Items.RemoveAt(_cmbFilter.Items.Count – 1);
    }
    _cmbFilter.SelectedIndex = 0;
    }

    }
    </i>

    <b>
    This would be my control template for the DataCell:
    </b>
    <i>
    <ControlTemplate x:Key=”cellFilter”
    TargetType=”local:DataCellFilter”>
    <Border Background=”{TemplateBinding Background}”
    BorderBrush=”{TemplateBinding BorderBrush}”
    BorderThickness=”{TemplateBinding BorderThickness}”
    Padding=”{TemplateBinding Padding}”>
    <ContentPresenter>
    <ContentPresenter.Content>
    <ComboBox Width =”{TemplateBinding Width}” x:Name =”cmbFilter” />
    </ContentPresenter.Content>
    </ContentPresenter>
    </Border>
    <ControlTemplate.Triggers>
    <DataTrigger Binding=”{Binding RelativeSource={RelativeSource Self},

    Imported from legacy forums. Posted by abatti (had 4231 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.