XAML 如何在使用Telerik RadGridView控件时定义自己的过滤器?

7rtdyuoh  于 2022-12-28  发布在  其他
关注(0)|答案(1)|浏览(159)

bounty将在2天后过期。回答此问题的人有资格获得+300的声望奖励。Dominique希望吸引更多人关注此问题。

我正在开发一个C#应用程序,尝试创建自己的过滤机制来过滤Telerik RadGridView控件。
我的RadGridView控件看起来如下所示(在xaml文件中,我保留了一些开放的行,用于显示我定义过滤器的部分):

<telerik:RadGridView x:Name="PartsGridView"
                     AutoGenerateColumns="False"
                     CanUserFreezeColumns="False"
                     FilteringMode="Popup"
                     FilterOperatorsLoading="Grid_FilterOperatorsLoading"
                     ItemsSource="{Binding PagedSource, ElementName=PartsRadDataPager}"
                     IsSynchronizedWithCurrentItem="False"
                     SelectedItem="{Binding SelectedPart}"
                     SelectionMode="Single"
                     ShowInsertRow="False"
                     EnableRowVirtualization="True"
                     RowLoaded="PartsGridView_RowLoaded"
                     FieldFilterEditorCreated="FieldFilterEditorCreated"
                     IsFilteringAllowed="True"
                     DistinctValuesLoading="PartsGridView_DistinctValuesLoading"
                     Grouped="PartsGridView_Grouped">
    <telerik:RadGridView.GroupDescriptors>
        <telerik:GroupDescriptor Member="Name" DisplayContent="Article"/>
    </telerik:RadGridView.GroupDescriptors>
    <telerik:RadGridView.FilterDescriptors/>
    <telerik:RadGridView.Columns>
        ...
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Article.CustomerReference}" 
                                    Header="Client"
                                    IsReadOnly="True"
                                    Name="Client">
        
            <telerik:GridViewDataColumn.FilteringControl>
                <local:Myfilter Column="{Binding ElementName=Client}"/>
            </telerik:GridViewDataColumn.FilteringControl>
            
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

相应的源代码如下所示(我只展示了OnFilter,它的想法是将过滤的可能性扩展到正则表达式,但让我们首先开始使正则过滤器工作):

public partial class MyFilter : System.Windows.Controls.UserControl, 
                                IFilteringControl
{
    private GridViewDataColumn column;
    private CompositeFilterDescriptor compositeFilter;
    private Telerik.Windows.Data.FilterDescriptor textFilter;

    private void OnFilter(object sender, RoutedEventArgs e)
    {
        //var reg = new Regex(textBox.Text.Replace("*", ".*"));

        // var descriptor = new FilterDescriptor<string> 
        //                  { FilteringExpression = o => reg.IsMatch(o) };

        compositeFilter = new CompositeFilterDescriptor();

        textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
            , Telerik.Windows.Data.FilterOperator.IsEqualTo
            , null);

        compositeFilter.FilterDescriptors.Add(textFilter);

        textFilter.Value = TextBox;

        if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
        {
            Column.DataControl.FilterDescriptors.Add(compositeFilter);
        }

        IsActive = true;
    }

在调试时,我进入OnFilter()函数,但不是仅仅过滤,而是在RadGridView中看不到任何数据。
有人知道我哪里做错了吗?

**编辑:**如果问题不清楚,请立即询问更多信息。

dohp0rv5

dohp0rv51#

试试这个:

private void OnFilter(object sender, RoutedEventArgs e)
{
    //var reg = new Regex(textBox.Text.Replace("*", ".*"));

    // var descriptor = new FilterDescriptor<string> 
    //                  { FilteringExpression = o => reg.IsMatch(o) };

    //compositeFilter = new CompositeFilterDescriptor();

    textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
        , Telerik.Windows.Data.FilterOperator.IsEqualTo
        , null);

    //compositeFilter.FilterDescriptors.Add(textFilter);

    textFilter.Value = TextBox;

    if (!Column.DataControl.FilterDescriptors.Contains(textFilter))
    {
        Column.DataControl.FilterDescriptors.Add(textFilter);
    }

    IsActive = true;
}

相关问题