【问题标题】:dataGrid filter from combobox doesn't work来自组合框的 dataGrid 过滤器不起作用
【发布时间】:2016-01-05 16:00:40
【问题描述】:

我试图通过从两个组合框中选择两个值来过滤 dataGrid,但选择的值将值 0 发送到属性。

var idShop = 商店;等于零

var idSupplier = 供应商;等于零

视图模型

public class ConsultInvoiceViewModel:ViewModelBase
    {
        public Context ctx = new tContext();

        private ICollectionView _dataGridCollection;
        private string _filterString;
        private ObservableCollection<Invoice> invoiceCollection = new ObservableCollection<Invoice>();


        public ConsultInvoiceViewModel()
        {
            DataGridCollection = CollectionViewSource.GetDefaultView(Get());
            //DataGridCollection.Filter = new Predicate<object>(Filter);
        }

        public ICollectionView DataGridCollection
        {
            get
            { 
                return _dataGridCollection; 
            }
            set 
            { 
                _dataGridCollection = value; 
                OnPropertyChanged("DataGridCollection"); }
        }


        private void FilterCollection()
        {
            if (_dataGridCollection != null)
            {
                _dataGridCollection.Refresh();
            }
        }




        private void Search()
        {
            var idShop = Shop;
            var idSupplier = Supplier;

            var inv = (from i in ctx.Invoices
                       where i.shop == idShop 
                       && i.supplier == idSupplier
                       select i).SingleOrDefault();

            invoiceCollection.Clear();
            invoiceCollection.Add(inv);
            FilterCollection();

        }

        private ObservableCollection<Invoice> Get()
        {
            ctx.Invoices.ToList().ForEach(invoice => ctx.Invoices.Local.Add(invoice));
            invoiceCollection = ctx.Invoices.Local;
            return invoiceCollection;
        }
        private void GetShop()
        {
            ctx.shops.ToList().ForEach(shop => ctx.shops.Local.Add(shop));
            SShop = ctx.shops.Local;
        }

        private void GetSupplier()
        {
            ctx.foodSuppliers.ToList().ForEach(supplier => ctx.foodSuppliers.Local.Add(supplier));
            FoodSupplier = ctx.foodSuppliers.Local;
        }

        private IList<foodSupplier> supplier;

        public IList<foodSupplier> FoodSupplier
        {
            get
            {
                if (supplier == null)
                GetSupplier();
                return supplier;
            }
            set
            {
                supplier = value;
                OnPropertyChanged("FoodSupplier");
            }
        }

        private IList<shop> shop;

        public IList<shop> SShop
        {
            get
            {
                if(shop == null)
                GetShop();
                return shop;
            }
            set
            {
                shop = value;
                OnPropertyChanged("SShop");
            }
        }

        private int _shop;

        public int Shop
        {
            get 
            { 
                return _shop; 
            }
            set 
            {
                _shop = value;
                OnPropertyChanged("Shop");
            }
        }

        private int _supplier;

        public int Supplier
        {
            get
            {
                return _supplier;
            }
            set
            {
                _supplier = value;
                OnPropertyChanged("Supplier");
            }
        }





        #region "Command"

        private ICommand searchCommand;


        public ICommand SearchCommand
        {
            get
            {
                return searchCommand ?? (searchCommand = new RelayCommand(p => this.Search(), p => this.CanSearch()));
            }
        }

        private bool CanSearch()
        {
           if (Supplier != 0 && Shop != 0)
                return true;
            else
                return false;

        }

        #endregion
    }

查看

<StackPanel Orientation="Horizontal">
                <Label Content="Shop"/>
                <ComboBox Name="shopComboBox"
                          Margin="5" 
                          ItemsSource="{Binding SShop}" 
                          DisplayMemberPath="shop1" Width="73"
                          SelectedItem="{Binding Shop, Mode=OneWayToSource}" 
                          SelectedValuePath="idshop" SelectionChanged="shopComboBox_SelectionChanged"/>
                <Label Content="Supplier"/>
                <ComboBox Name="supplierComboBox"
                          Margin="5" 
                          ItemsSource="{Binding FoodSupplier}" 
                          DisplayMemberPath="supplier"
                          SelectedItem="{Binding Supplier, Mode=OneWayToSource}" 
                          SelectedValuePath="idfoodSupplier"
                          Width="71"/>
                <Label Content="Shop"/>
                <Button Content="Search" 
                            Margin="5"
                            Command="{Binding SearchCommand}"/>
            </StackPanel>


            <StackPanel Orientation="Vertical">
                <DataGrid Margin="5" ItemsSource="{Binding DataGridCollection}" AutoGenerateColumns="False" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="SuppNb" Binding="{Binding suppInvNumber}" Width="*"/>
                        <DataGridTextColumn Header="Shop" Binding="{Binding shop1.shop1}" Width="*"/>
                        <DataGridTextColumn Header="Date" Binding="{Binding date}" Width="*"/>
                        <DataGridTextColumn Header="Supplier" Binding="{Binding foodSupplier.supplier}" Width="*"/>
                        <DataGridTextColumn Header="Ref Supplier" Binding="{Binding refSupp}" Width="*"/>
                        <DataGridTextColumn Header="Unit" Binding="{Binding unit}" Width="*"/>
                        <DataGridTextColumn Header="Quantity" Binding="{Binding quantity}" Width="*"/>
                        <DataGridTextColumn Header="Prix/MOQ" Binding="{Binding unitPrice}" Width="*"/>
                        <DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice}" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>
            </StackPanel>

【问题讨论】:

    标签: c# wpf combobox datagrid


    【解决方案1】:

    在 SelectedItem 处绑定是错误的。将 SelectedValue 与 SelectedValuePath 一起使用。

    SelectedItem 只能绑定到一个项目(对象)。 而 SelectedValue 可以绑定到 item 的 SelectedValuePath 指定的值。

    只需像下面这样更改它,您应该能够从组合框中获得结果。

                <ComboBox Name="supplierComboBox"
                          Margin="5" 
                          ItemsSource="{Binding FoodSupplier}" 
                          DisplayMemberPath="supplier"
                          SelectedValue="{Binding Supplier, Mode=OneWayToSource}" 
                          SelectedValuePath="idfoodSupplier"
                          Width="71"/>
    

    【讨论】:

    • 别这么说。我们都去过那里。仅仅因为疏忽而被困住了几天。大声笑
    【解决方案2】:

    您需要将 Comoboxes 绑定到 ViewModel 中的某个命令,以便 ViewModel 知道您选择了什么?

    或者,您需要将 ComboBoxes 选定项作为参数传递给 Search 命令,以便在您的 Search 方法中访问用户选择的值。

    现在你是直接访问 Shop,这是没有意义的。您假设当您在 ShopComboBox 中选择一个项目时,ViewModel 中的 Shop 变量将获得该值。这是错误的。

    当您将 ObservableCollection 绑定到 DataGrid 并在 DataGrid 中选择一行时,ObservableCollection 对这个选择一无所知,但您的 DataGrid 知道。

    【讨论】:

    • 我想将两个组合框绑定到两个属性。当我点击一个按钮时,Search() 函数会进行过滤。
    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2020-06-06
    • 2021-04-02
    相关资源
    最近更新 更多