我正在尝试使用复合滤镜(即多个滤镜条件)过滤对象列表视图,但默认的高亮文本渲染器仅渲染第一个滤镜的文本。
有没有办法让它适用于所有过滤器,或者更好的是为每个过滤器使用多个文本渲染器?
我正在使用控制字符(&&和||)来分隔搜索字词
一些C& P代码来说明
public void Filter (string txt, MatchKind matchKind) {
bool filterByAll;
IEnumerable<string> terms = SplitSearchTerms(txt, out filterByAll);
List<IModelFilter> modelFilters = new List<IModelFilter>();
foreach (string term in terms) {
IModelFilter filter;
switch (matchKind) {
case MatchKind.Contains:
default:
filter = TextMatchFilter.Contains(_olv, term);
break;
case MatchKind.Prefix:
filter = TextMatchFilter.Prefix(_olv, term);
break;
case MatchKind.Regex:
filter = TextMatchFilter.Regex(_olv, term);
break;
}
modelFilters.Add(filter);
}
CompositeFilter compositeFilter;
if (filterByAll) {
compositeFilter = new CompositeAllFilter(modelFilters);
} else {
compositeFilter = new CompositeAnyFilter(modelFilters);
}
//Only highlights text from the first filter
HighlightTextRenderer renderer = _olv.DefaultRenderer as HighlightTextRenderer;
if (renderer != null) {
SolidBrush brush = renderer.FillBrush as SolidBrush ?? new SolidBrush(Color.Transparent);
if (brush.Color != Color.LightSeaGreen) {
brush.Color = Color.LightSeaGreen;
renderer.FillBrush = brush;
_olv.DefaultRenderer = renderer;
}
} else {
MessageBox.Show(@"Renderer is null!");
}
_olv.ModelFilter = compositeFilter;
}
答案 0 :(得分:1)
此解决方案适合我。多列支持仍支持突出显示。
var filters = new List<IModelFilter>();
TextMatchFilter highlightingFilter = null;
if (!String.IsNullOrEmpty(txtSearch.Text))
{
var words = txtSearch.Text.Trim().Split(null);
highlightingFilter = TextMatchFilter.Contains(ListView, words);
foreach (var word in words)
{
var filter = TextMatchFilter.Contains(ListView, word);
filters.Add(filter);
}
}
var compositeFilter = new CompositeAllFilter(filters);
ListView.ModelFilter = highlightingFilter;
ListView.AdditionalFilter = compositeFilter;
ListView.DefaultRenderer = new HighlightTextRenderer(highlightingFilter);