我试图通过从两个组合框中选择两个值来过滤dataGrid,但是所选的值会将值0发送到属性。
var idShop = Shop;等于零
var idSupplier = Supplier;等于零
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>
答案 0 :(得分:2)
在SelectedItem上绑定是错误的。请将SelectedValue与SelectedValuePath一起使用。
SelectedItem只能绑定到项目(对象)。 而SelectedValue可以绑定到项目的SelectedValuePath指定的值。
只需像下面一样进行更改,您就可以从组合框中获得结果。
<ComboBox Name="supplierComboBox"
Margin="5"
ItemsSource="{Binding FoodSupplier}"
DisplayMemberPath="supplier"
SelectedValue="{Binding Supplier, Mode=OneWayToSource}"
SelectedValuePath="idfoodSupplier"
Width="71"/>
答案 1 :(得分:0)
您需要将Comoboxes绑定到ViewModel中的某个命令,以便ViewModel知道您选择了什么?
或者,您需要将ComboBoxes选定的项目作为参数传递给搜索命令,以便在您的搜索方法中可以访问用户选择的值。
现在你正在直接访问Shop,这是毫无意义的。您假设当您在ShopComboBox中选择项目时,ViewModel中的Shop变量将获得该值。这是错误的。
当您将ObservableCollection绑定到DataGrid,并在DataGrid中选择一行时,ObservableCollection不知道有关此选择的任何内容,但您的DataGrid会这样做。