WPF 继承Combox的下拉过滤控件

4 篇文章 0 订阅

这个还有一些问题,需要花点时间去整理一下

                 
<UserControl x:Class="WPFMVVM.Controls.CommodityEdit"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFMVVM.Controls">
    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type local:CommodityEdit}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal" Grid.Row="0">
                    <TextBox x:Name="txt_Input" Focusable="True" Width="Auto" MinWidth="200" Text="{Binding InputValue,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                    </TextBox>
                </StackPanel>

                <!--数据匹配列表-->
                <Popup x:Name="dataList"  StaysOpen="True" PlacementTarget="{Binding ElementName=txt_Input}" AllowsTransparency="True" >
                    <ListView x:Name="_listView" Focusable="True" AllowDrop="True" IsEnabled="True" Width="Auto" Height="Auto"
                                      SelectedValue="{Binding SelctedInstance,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                      ItemsSource="{Binding DataCollectionView,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" 
                                      >
                        <ListView.View>
                            <GridView AllowsColumnReorder="True">
                                <GridViewColumn Header="书名" DisplayMemberBinding="{Binding Name}"/>
                                <GridViewColumn Header="价格" DisplayMemberBinding="{Binding Price}"/>
                                <GridViewColumn Header="作者" DisplayMemberBinding="{Binding Publisher}"/>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Popup>

            </Grid>
            <ControlTemplate.Triggers>
                <Trigger SourceName="txt_Input" Property="IsFocused" Value="True">
                    <Setter TargetName="dataList" Property="IsOpen" Value="True"></Setter>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition SourceName="txt_Input" Property="IsFocused" Value="False"></Condition>
                        <Condition SourceName="dataList" Property="IsFocused" Value="False"></Condition>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter TargetName="dataList" Property="IsOpen" Value="False"></Setter>
                    </MultiTrigger.Setters>

                </MultiTrigger>

                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition SourceName="txt_Input" Property="IsFocused" Value="False"></Condition>
                        <Condition SourceName="dataList" Property="IsFocused" Value="True"></Condition>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter TargetName="dataList" Property="IsOpen" Value="True"></Setter>
                    </MultiTrigger.Setters>

                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

C#代码

public partial class CommodityEdit:UserControl, INotifyPropertyChanged
    {
        //public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register(
        //   "InputValue", typeof(string), typeof(CommodityEdit), new PropertyMetadata(string.Empty, OnContentPropertyChanged),
        //   OnValidateCallBack);


        //private static void OnContentPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        //{
        //}

        //private static bool OnValidateCallBack(object value)
        //{

        //    return true;
        //}

        //public string InputValue
        //{
        //    get { return (string)GetValue(ContentProperty); }
        //    set { SetValue(ContentProperty, value); }
        //}


        public CommodityEdit()
        {
            InitializeComponent();
            DataCollection = new ObservableCollection<BookViewModel>()
            {
                new BookViewModel(){Name = "C语言程序设计",Price =39.9,Publisher = "谭浩强"}
            };
            DataCollectionView = CollectionViewSource.GetDefaultView(DataCollection);
            DataCollectionView.Filter = new Predicate<object>(OnFilter);

            this.PropertyChanged += CommodityEdit_PropertyChanged;

           
        }


        void CommodityEdit_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("SelctedInstance"))
            {
                if (SelctedInstance != null)
                {
                    InputValue = SelctedInstance.Name;
                    DataCollectionView.Refresh();
                }
            }
        }

        private string inputValue;

        public string InputValue
        {
            get { return inputValue; }
            set
            {
                if (inputValue != value)
                {
                    inputValue = value;
                    OnPropertyChanged("InputValue");
                    DataCollectionView.Refresh();
                }
            }
        }

        private bool OnFilter(object mod)
        {
            //Console.WriteLine(DataCollectionView.Count);
            var book=mod as BookViewModel;
            if (book != null && !String.IsNullOrWhiteSpace(InputValue))
            {
                return book.Name.Contains(InputValue) || book.Name.Equals(InputValue, StringComparison.CurrentCultureIgnoreCase);
            }
            else
                return true;
        }
        private ICollectionView dataCollectionView;
        private ObservableCollection<BookViewModel> dataCollection;

        public ICollectionView DataCollectionView
        {
            get { return dataCollectionView; }
            set
            {
                if (dataCollectionView != value)
                {
                    dataCollectionView = value;
                    OnPropertyChanged("DataCollectionView");
                };
            }
        }

        public ObservableCollection<BookViewModel> DataCollection
        {
            get { return dataCollection; }
            set
            {
                if (dataCollection != value)
                {
                    dataCollection = value;
                    OnPropertyChanged("DataCollection");
                };
            }
        }

        private BookViewModel selctedInstance;

        public BookViewModel SelctedInstance
        {
            get { return selctedInstance; }
            set
            {
                if (selctedInstance != value)
                {
                    selctedInstance = value;
                    OnPropertyChanged("SelctedInstance");
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值