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

时间:2014-03-11 08:02:23

标签: c# wpf mvvm devexpress

我有一个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将实现过滤数据的自定义逻辑)< / strong>如下:

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。请让我知道如何解决它还是有更好的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

嗯,得到了我的回答,这里是否有其他人面临同样的问题。我们只需要通过继承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);
        }
    }
}