Reputation: 136
I m trying to filter a dataGrid by selecting two values from two comboBoxes, but the values selected send value 0 to the property.
var idShop = Shop; equal to zero
var idSupplier = Supplier; equal to zero
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>
Upvotes: 1
Views: 55
Reputation: 2781
It's wrong to bind at SelectedItem. Use SelectedValue with SelectedValuePath instead.
SelectedItem can only be binded to an item (object). While SelectedValue can be binded to the value specified by the SelectedValuePath of the item.
Just change it like below and you should be able to get your result from the combobox.
<ComboBox Name="supplierComboBox"
Margin="5"
ItemsSource="{Binding FoodSupplier}"
DisplayMemberPath="supplier"
SelectedValue="{Binding Supplier, Mode=OneWayToSource}"
SelectedValuePath="idfoodSupplier"
Width="71"/>
Upvotes: 3
Reputation: 9827
You need to either bind Comoboxes to some command in your ViewModel, so that your ViewModel knows what you have selected ?
Or, you need to pass ComboBoxes selected items as parameters to Search command, so that in your Search method you can access User selected values.
Right now you are accessing Shop directly, which is meaningless. You are assuming that when you select an item in ShopComboBox, Shop variable in ViewModel will get that value. This is wrong.
When you bind say an ObservableCollection to DataGrid, and you select a row in DataGrid, ObservableCollection doesn't know anything about this selection but your DataGrid does.
Upvotes: 0