1

第一:我们使用infragistics xamdatagrid 11.1.20111.2053

我们的问题:

我们使用带有通用列表的网格。所以它非常动态,必须为任何情况做好准备。我们为每个字段类型设置了SortComparer、FilterComparer、编辑器类型、Edita 类型和样式编辑器。对于模型的某些属性,我们使用特殊的 TypeConverter。例如,在一个单元格中,某些值无法显示。

0 = string.Empty
1 = 1
2 = 2

第一个解决方案,我们只使用类型转换器和特殊的排序比较器:

public class HideZeroIntEntryConverter : Int32Converter
{
  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
    if (value is int) {
      if (destinationType == typeof(string)) {
        return ((int)value != 0) ? value.ToString() : string.Empty;
      }
      return ((int)value != 0) ? value : Binding.DoNothing; // this is the best solution to tell the grid the cell is empty
    }
    return base.ConvertTo(context, culture, value, destinationType);
  }
}

如果我们不决定过滤,这很有效,但是如果我们想过滤值,我们会在过滤器下拉项中看到丑陋的“Binding.DoNothing”(排序和过滤也是错误的)。此外,我们不能过滤“0”,因为我们转换器说 string.empty...

第二种解决方案,我们使用特殊的 XamTextEditor:

public class HideZeroIntEntryTextEditor : XamTextEditor
{
  public HideZeroIntEntryTextEditor() {
    this.ValueToDisplayTextConverter = new HideZeroIntEntryValueConverter();
  }
}

public class HideZeroIntEntryValueConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value is int) {
      if (targetType == typeof(string)) {
        return ((int)value != 0) ? value.ToString() : string.Empty;
      }
      // this never happens
      return ((int)value != 0) ? value : Binding.DoNothing;
    }
    // this never happens
    return targetType == typeof(string) && value != null ? value.ToString() : value;
  }
}

和字段设置

  field.Settings.EditAsType = typeof(int);

  field.Converter = null;
  field.Settings.EditorType = typeof(HideZeroIntEntryTextEditor);
  field.Settings.SortComparer = GenericComparer<int>.Instance;
  field.Settings.FilterComparer = GenericComparer<int>.Instance;
  field.Settings.GroupByComparer = GroupByRecordComparer<int>.Instance;

现在我们可以过滤到“0”,即使它没有出现在列表中。

但是,在这两种情况下,我们都不能通过空条目进行过滤,因为它实际上并不存在!虽然我们想!我们认为,如果我们可以制作自己的特殊过滤器,则可能是这样。但不幸的是,这并不容易。是的,我们可以删除特殊的过滤器空白和非空白,但适用于所有网格。要覆盖的特殊过滤器非常复杂,甚至不正确。

好的,我们想看到空单元格,并想要过滤这个单元格,但是特殊的过滤器不能正常工作!

我们能做什么,有什么想法吗?

这是基础设施的问题

4

1 回答 1

3

我在这里的 Infragistics 论坛上用示例回答了这个问题。

当字段的编辑器为 HideZeroIntEntryTextEditor 时,您可以使用 RecordFilterDropDownPopulating 事件来删除“(空白)”的现有 FilterDropDownItem,并将其替换为值 0 的自定义项。下面的代码实现了这一点:

void XamDataGrid1_RecordFilterDropDownPopulating(
  object sender, 
  //Infragistics.Windows.DataPresenter.Events.
  RecordFilterDropDownPopulatingEventArgs e) 
{
  if (e.Field.EditorTypeResolved == typeof(HideZeroIntEntryTextEditor)) {
    bool found = false;
    int index = 0;
    while (index < e.DropDownItems.Count && !found) {
      FilterDropDownItem item = e.DropDownItems[index];
      if (item.DisplayText == "(Blanks)") {
        e.DropDownItems.Remove(item);
        e.DropDownItems.Insert(index, new FilterDropDownItem(
          new ComparisonCondition(ComparisonOperator.Equals, 0), "(Blanks)"));
        found = true;
      }
      index++;
    }
  }
}
于 2012-02-10T19:40:37.507 回答