public ObservableCollection<Person> FilteredCollection
{
get
{
return new ObservableCollection<Person>(PersonCollection.Where(i => FilteredCheck(i)));
}
}
public bool FilteredCheck(DemandHead item)
{
bool Accept=true;
// организовываем проверку как вашей душе удобно
}
// И дальше при изменении фильтра оповещаем View об изменении данных
public bool IsFilterEnabled
{
get
{
return _IsFilterEnabled;
}
set
{
_IsFilterEnabled = value;
base.OnPropertyChanged("IsFilterEnabled");
base.OnPropertyChanged("FilteredCollection");
}
}
public DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
foreach (T item in items)
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
} return tb;
}
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}
private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
{
cgAssortment cgA = e.Item as cgAssortment;
bool isAccept = true;
if (cgA != null)
{
try
{
isAccept = isAccept && (!this.cbOnLink.IsChecked.Value || (this.cbOnLink.IsChecked == true && (cgA.Group5GUID == null)));
isAccept = isAccept && (!this.cbInMatrix.IsChecked.Value || (this.cbInMatrix.IsChecked == true && (cgA.InMatrix == true)));
isAccept = isAccept && (!this.dgGoodsAllF1.IsChecked.Value || (this.dgGoodsAllF1.IsChecked.Value && cgA.Code.ToString() == this.dgGoodsAllT1.Text));
isAccept = isAccept && (!this.dgGoodsAllF2.IsChecked.Value || (this.dgGoodsAllF2.IsChecked.Value && (cgA.NameLong.ToUpper().Contains(this.dgGoodsAllT2.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF3.IsChecked.Value || (this.dgGoodsAllF3.IsChecked.Value && (cgA.cgGroup2.NameLong.ToUpper().Contains(this.dgGoodsAllT3.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF4.IsChecked.Value || (this.dgGoodsAllF4.IsChecked.Value && cgA.cgGroup3 != null && (cgA.cgGroup3.NameLong.ToUpper().Contains(this.dgGoodsAllT4.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF5.IsChecked.Value || (this.dgGoodsAllF5.IsChecked.Value && cgA.cgGroup4 != null && (cgA.cgGroup4.NameLong.ToUpper().Contains(this.dgGoodsAllT5.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF6.IsChecked.Value || (this.dgGoodsAllF6.IsChecked.Value && cgA.cgGroup5 != null && (cgA.cgGroup5.NameLong.ToUpper().Contains(this.dgGoodsAllT6.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF7.IsChecked.Value || (this.dgGoodsAllF7.IsChecked.Value && cgA.cgGroup6 != null && (cgA.cgGroup6.NameLong.ToUpper().Contains(this.dgGoodsAllT7.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF8.IsChecked.Value || (this.dgGoodsAllF8.IsChecked.Value && cgA.cgGroup7 != null && (cgA.cgGroup7.NameLong.ToUpper().Contains(this.dgGoodsAllT8.Text.ToUpper()))));
isAccept = isAccept && (!this.dgGoodsAllF9.IsChecked.Value || (this.dgGoodsAllF9.IsChecked.Value && cgA.cgGroup8 != null && (cgA.cgGroup8.NameLong.ToUpper().Contains(this.dgGoodsAllT9.Text.ToUpper()))));
}
catch (Exception er )
{
MessageBox.Show(er.Message);
}
e.Accepted = isAccept;
}
}