我正在使用来自 Infragistics 的 XamGrid,并且我按照以下页面中的示例创建了自定义的按钮列类:
我的目标是创建一个可以过滤的自定义图像列类。
我将 XAML 标记定义为:
<util:FilterableImageColumn
Key="Severity"
ValueConverter="{StaticResource EventSeverityToImageConverter}"
...>
这意味着我创建了一个名为 FilterableImageColumn 的自定义列。IE
public class FilterableImageColumn : Column
此标签还有一个与之关联的名为 EventSeverityToImageConverter 的 ValueConverter:
ValueConverter="{StaticResource EventSeverityToImageConverter}"
这样做是根据收到的值创建一个特定的图像。它的定义如下:
public class EventSeverityToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
BitmapImage severityIcon = null;
try
{
if (value != null)
{
Uri uri = null;
Severities source = (Severities)value;
switch (source)
{
case Severities.Informational:
uri = new Uri("image1");
break;
case Severities.Warning:
uri = new Uri("image2");
break;
case Severities.Critical:
default:
uri = new Uri("image3");
break;
}
if (uri != null)
{
severityIcon = new BitmapImage();
severityIcon.BeginInit();
severityIcon.UriSource = uri;
severityIcon.EndInit();
}
}
}
catch (Exception ex)
{
LoggingProvider.WriteExceptionLogEntry(ex);
}
return severityIcon;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
现在,按照给定的链接中的说明,我们创建一个 FilterableImageColumnContentProvider 而不是 ButtonColumnContentProvider。(我还从 ButtonColumn 类中获取了相关 ContentBinding 属性的链接示例中给出的默认代码,并将其移至我的 FilterableImageColumn 类)
FilterableImageColumnContentProvider : ColumnContentProviderBase
通过执行以下操作,我设法将 ResolveDisplayElement 更改为显示图像而不是按钮:
public override FrameworkElement ResolveDisplayElement(Cell cell, Binding cellBinding)
{
_column = (FilterableImageColumn)cell.Column;
Binding binding = new Binding{ Source = _image.Source};
if (_column.Content == null)
{
binding.Mode = BindingMode.OneWay;
if (_column.ContentBinding != null)
{ this._image.SetBinding(Image.SourceProperty,_column.ContentBinding);
}
else
{
this._image.SetBinding(Image.SourceProperty, binding);
}
}
else
{
binding = new Binding("Content");
binding.Source = _column;
this._image.SetBinding(Button.ContentProperty, binding);
}
return _image;
}
我已经暂时对 _image 进行了硬编码,以查看它是否会使用上述内容出现,并且确实如此。我已经硬编码如下:
Image _image;
public FilterableImageColumnContentProvider()
{
Uri uri = new Uri("myImg");
_image = new Image();
_image.BeginInit();
_image.Source = new BitmapImage(uri);
_image.EndInit();
}
我的问题是,我如何才能访问我的 ValueConverter (EventSeverityToImageConverter) 从我的自定义 FilterableImageColumn 或 FilterableImageColumnContentProvider 类中创建的内容?
我基本上想从我的 FilterableImageColumn 或 FilterableImageColumnContentProvider 中做 EventSeverityToImageConverter 中发生的事情。例如,我想根据收到的内容在图像之间切换。
目前,使用我提供的示例代码,该列仅显示我硬编码的图像(这是有道理的),尽管它确实知道我收到的基础严重性,这在过滤器中很明显。
显然信息在某处,但我不知道如何从我的 FilterableImageColumn/FilterableImageColumnContentProvider 中访问它,以便在该严重性类型(我们看到的关键或信息字符串)上切换图像。
任何建议,将不胜感激。
基本上如果我能改变
Binding binding = new Binding{ Source = _image.Source};
到我在 EventSeverityImageConverter 中创建的 severityIcon。
Binding binding = new Binding{ Source = _severityIcon};
我有我的解决方案。