具有复合过滤器的对象列表视图突出显示渲染器
Object List View Highlight Renderer with Composite Filters
我正在尝试使用复合过滤器(即多个过滤条件)来过滤对象列表视图,但默认的突出显示文本呈现器仅呈现第一个过滤器的文本。
有没有办法让它适用于所有过滤器,或者更好的是使用多个文本渲染器,每个过滤器一个?
我使用控制字符(&& 和 ||)来分隔搜索词
一些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;
}
这个解决方案适合我。多列支持仍然支持突出显示。
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);
我正在尝试使用复合过滤器(即多个过滤条件)来过滤对象列表视图,但默认的突出显示文本呈现器仅呈现第一个过滤器的文本。
有没有办法让它适用于所有过滤器,或者更好的是使用多个文本渲染器,每个过滤器一个?
我使用控制字符(&& 和 ||)来分隔搜索词
一些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;
}
这个解决方案适合我。多列支持仍然支持突出显示。
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);