刷新 ObjectDataProvider 时视图丢失

发布于 2024-07-10 19:54:53 字数 625 浏览 9 评论 0原文

我正在基于 Josh Smith 的示例在列表上实现文本框过滤器 http://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox。 基本上,它将视图上的过滤器设置为检查搜索框中文本的委托。 我像这样连接过滤器:

var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView.View, SearchTextBox);

稍后,当我刷新 ObjectDataProvider 时,过滤器丢失了。 我注意到刷新后 pickerView.View 有不同的哈希码。 数据刷新时是否会重新创建所有视图? 这是否意味着每当我调用 ObjectDataProvider.Refresh() 时我都应该重新连接过滤器? 有没有一些更智能的方法来安装这个过滤器,而不需要保姆?

I'm implementing a textbox filter on a list based on Josh Smith's example at http://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox. Basically, it sets the Filter on the view to a delegate that checks against the text in the search box. I hook up the filter like so:

var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView.View, SearchTextBox);

Later, when I refresh the ObjectDataProvider, the filter is lost. I've noticed that pickerView.View has a different hashcode after the refresh. Are all the views recreated when the data refreshes? Does that mean I should reattach the filter again whenever I call ObjectDataProvider.Refresh()? Is there some smarter way to install this filter that wouldn't require babysitting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-07-17 19:54:54

您说得对,当设置 CollectionViewSource.Source 时, CollectionViewSource.View 将被替换。

解决方案是使用 CollectionViewSource.Filter< /a> 事件而不是 CollectionView.Filter 属性。 当你的视图消失时,它会一直存在。

您只需对 Josh Smith 的 TextSearchFilter 类:

public class TextSearchFilter
{
    public TextSearchFilter( 
        CollectionViewSource filteredView, 
        TextBox textBox )
    {
        string filterText = "";

        filteredView.Filter += delegate( object obj, FilterEventArgs e )                
        {
            if( String.IsNullOrEmpty( filterText ) )
                e.Accepted = true;

            string str = e.Item as string;
            if( String.IsNullOrEmpty( str ) )
                e.Accepted = false;

            int index = str.IndexOf(
                filterText,
                0,
                StringComparison.InvariantCultureIgnoreCase );

            e.Accepted = index > -1;
        };          

        textBox.TextChanged += delegate
        {
            filterText = textBox.Text;
            filteredView.View.Refresh();
        };
    }
}

您的连接代码将变为:

var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView, SearchTextBox);

You're right in saying that CollectionViewSource.View will be replaced when CollectionViewSource.Source is set.

The solution is to use the CollectionViewSource.Filter event instead of the CollectionView.Filter property. This will stick around when your View goes away.

You can do this with minimal changes to Josh Smith's TextSearchFilter class:

public class TextSearchFilter
{
    public TextSearchFilter( 
        CollectionViewSource filteredView, 
        TextBox textBox )
    {
        string filterText = "";

        filteredView.Filter += delegate( object obj, FilterEventArgs e )                
        {
            if( String.IsNullOrEmpty( filterText ) )
                e.Accepted = true;

            string str = e.Item as string;
            if( String.IsNullOrEmpty( str ) )
                e.Accepted = false;

            int index = str.IndexOf(
                filterText,
                0,
                StringComparison.InvariantCultureIgnoreCase );

            e.Accepted = index > -1;
        };          

        textBox.TextChanged += delegate
        {
            filterText = textBox.Text;
            filteredView.View.Refresh();
        };
    }
}

Your hookup code then becomes:

var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView, SearchTextBox);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文