自定义搜索/过滤器在DevExpress LookUpEdit

本文关键字:DevExpress LookUpEdit 过滤器 搜索 自定义 | 更新日期: 2023-09-27 17:49:22

我有一个LookUpEdit控件,封装GridControl和DataPagerControl,从而实现分页功能,如下所示:

 <Test:LookUpEditEx CustomFilterCommand="{Binding FilterData}" Margin="10" Width="250" ItemsSource="{Binding Locations}" TabIndex="2"
                           NullText="{x:Static p:Resources.LblNone}" DisplayMember="LocationCode" SelectedItem="{Binding SelectedLocation}" AutoPopulateColumns="False"
                    HorizontalAlignment="Left" VerticalAlignment="Center" ValueMember="LocationId">
            <Test:LookUpEditEx.Buttons>
                <dxe:ButtonInfo ButtonKind="Simple" Content=" X " Command="{Binding Path=ClearLocation}" />
            </Test:LookUpEditEx.Buttons>
            <Test:LookUpEditEx.PopupContentTemplate>
                <ControlTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <dxg:GridControl Margin="5" Height="240" x:Name="PART_GridControl" Grid.Row="0">
                            <i:Interaction.Behaviors>
                                <Pager:CustomSortingBehavior CustomSortingCommand="{Binding SortLocations}" />
                            </i:Interaction.Behaviors>
                            <dxg:GridControl.View>
                                <dxg:TableView AllowColumnFiltering="False" IsColumnMenuEnabled="False" ShowGroupPanel="False" AutoWidth="True" />
                            </dxg:GridControl.View>
                            <dxg:GridControl.Columns>
                                <dxg:GridColumn FieldName="LocationCode" />
                                <dxg:GridColumn FieldName="ParentLocationCode" />
                            </dxg:GridControl.Columns>
                        </dxg:GridControl>
                        <dxe:DataPager x:Name="dataPager" AutoEllipsis="True" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Center"
                                             PageIndex="{Binding PageIndex, Mode=TwoWay}" Margin="5">
                            <i:Interaction.Behaviors>
                                <Pager:SourcesBehavior x:Name="LocationDataPagerSource" TotalSourcesCount="{Binding TotalLocationCount}"
                                                           Sources="{Binding Locations}" />
                            </i:Interaction.Behaviors>
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="PageIndexChanging">
                                    <local_interaction:EventToCommandEx Command="{Binding LocationPageIndexChangeCommand}"
                                                             EventArgsConverter="{StaticResource PageIndexChangeConverter}"  PassEventArgsToCommand="True" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </dxe:DataPager>
                    </Grid>
                </ControlTemplate>
            </Test:LookUpEditEx.PopupContentTemplate>
        </Test:LookUpEditEx>

我已经创建了一个自定义控件继承自LookUpEdit作为LookUpEditEx,并暴露了一个依赖属性"CustomFilterCommand"用于自定义过滤逻辑(每个ViewModel将实现过滤数据的自定义逻辑)如下:

using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid.LookUp;
using System.Windows;
using System.Windows.Input;
namespace ABC
{
    public class LookUpEditEx : LookUpEdit
    {
        public static readonly DependencyProperty CustomFilterCommandProperty =
        DependencyProperty.Register("CustomFilterCommand", typeof(ICommand), typeof(LookUpEditEx),
        new PropertyMetadata(null));
        public ICommand CustomFilterCommand
        {
            get
            {
                return (ICommand)GetValue(CustomFilterCommandProperty);
            }
            set
            {
                SetValue(CustomFilterCommandProperty, value);
            }
        }
        private override EditStrategyBase CreateEditStrategy()
        {
            return new FilteredLookUpEditStrategy(this, CustomFilterCommand);
        }
    }
}

和"FilteredLookUpEditStrategy"看起来像:

using DevExpress.Xpf.Grid.LookUp;
using System.Windows.Input;
namespace ABC
{
    public class FilteredLookUpEditStrategy : LookUpEditStrategy
    {
        private ICommand filterCommand = null;
        public FilteredLookUpEditStrategy(LookUpEdit editor, ICommand filterCommand)
            : base(editor)
        {
            this.filterCommand = filterCommand;
        }
        public override void AutoSeachTextChanged(string text)
        {
            this.filterCommand.Execute(string.Empty);
        }
    }
}

但是当我执行时,我得到的DependencyProperty"CustomFilterCommand"为NULL。请让我知道如何去解决它或有更好的方法?

谢谢!

自定义搜索/过滤器在DevExpress LookUpEdit

好吧,我得到了我的答案,这是如果别人面临同样的问题。我们只需要通过继承LookUpEdit来创建一个自定义控件,如下所示:

namespace ABC
{
    public class LookUpEditEx : LookUpEdit
    {
        public static readonly DependencyProperty CustomFilterCommandProperty =
        DependencyProperty.Register("CustomFilterCommand", typeof(ICommand), typeof(LookUpEditEx),
        new PropertyMetadata(null));
        public ICommand CustomFilterCommand
        {
            get
            {
                return (ICommand)GetValue(CustomFilterCommandProperty);
            }
            set
            {
                SetValue(CustomFilterCommandProperty, value);
            }
        }
        protected override void OnAutoSearchTextChanged(string displayText)
        {            
            if (CustomFilterCommand != null)
                CustomFilterCommand.Execute(displayText);
        }
    }
}