有可能以Predicate<T> to Expression<Func<T, bool>>某种方式转换?
我想使用我的ICollectionView的过滤器使用下一个IQueryable函数:
public static System.Linq.IQueryable<TSource> Where<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate)
Run Code Online (Sandbox Code Playgroud)
谢谢
我想要一个属性到当前的项目ICollectionView我该怎么办?将ICollectionView用于绑定到一个组合框,我怎么可以绑定其他控件到ICollectionView所选项目的?
我在XAML中有一个ItemsControl,我为每个组显示一个扩展器,这样我就可以扩展/折叠组.我想保持IsExpanded属性的状态(以及可能与组头的显示有关的其他设置).通常你只有一个包含属性的类并绑定到它.但是,该组的数据上下文是CollectionViewGroup.现在这个类不是很有帮助,因为它只给你Name属性和组中的项目(如果你只是想要一个标题,这可能很好,并且可能根据组中的项目数量或它们显示某种度量标准内容但不是,如果您只想存储有关组头UI状态的自定义数据).我想要做的是从这个类派生并将其他属性添加到我的派生类并绑定到它.但似乎没有任何简单的方法可以做到这一点.小组生成的所有细节似乎都隐藏在内部课程中,这非常令人沮丧.有没有人沿着实现ICollectionView自己的路线走下去(因此推测所有其他相关课程)?复制一切似乎是一项巨大的工作ListCollectionView只是为了能够创建一个自定义CollectionViewGroup类并绑定到它!谢谢.
如何从c#中的ICollectionView中删除项目?
Dictionary<int, String> entityDict;
public ICollectionView DictView { get; set; }
dictView = CollectionViewSource.GetDefaultView(entityDict.Values);
dictView.//No remove method
Run Code Online (Sandbox Code Playgroud)
谢谢
我想根据类型(类型AddPoint)过滤ObservableCollection到一个子集,并希望它按升序排序,没有重复.我的基类是ModelBase,w /子类AddPoint,Time,Repeat等...... ObservableCollection MotionSequenceCollection将以任何顺序填充这些类型,有些将是重复的.
我已经尝试了几次不同的时间,并在ICollectionView属性中显示它们,我从'拉出': 绑定集合的子集.
可观察的收藏品
private ObservableCollection<ModelBase> _motionSequenceCollection =
new ObservableCollection<ModelBase>();
public ObservableCollection<ModelBase> MotionSequenceCollection
{
get
{
return _motionSequenceCollection;
}
set
{
if (_motionSequenceCollection == value)
{
return;
}
var oldValue = _motionSequenceCollection;
_motionSequenceCollection = value;
// Update bindings, no broadcast
RaisePropertyChanged();
}
}
public ICollectionView Location
{
get
{
var location = CollectionViewSource.GetDefaultView(_motionSequenceCollection);
//DOES NOT WORK. PROBLEM: GetType() creates type of system.type() and AddPoint, which don't work. Need a cast, or something??
// found at https://stackoverflow.com/questions/9621393/bind-subset-of-collection …Run Code Online (Sandbox Code Playgroud) 我变得非常大ObservableCollection<MyItem>,我需要对它提供用户友好的过滤。
public static async Task RefilterViewAsync(this ItemsControl @this, Predicate<object> compareLogic)
{
await Task.Run(
() =>
{
var collectionView = CollectionViewSource.GetDefaultView(@this.ItemsSource);
if (collectionView.CanFilter)
{
collectionView.Filter = compareLogic;
}
else throw new InvalidOperationException("Filtering not supported...");
collectionView.Refresh();
});
}
Run Code Online (Sandbox Code Playgroud)
..问题是上面的代码由于某些原因不起作用。在 UI 线程上进行拟合大约需要 1 分钟。任何想法如何实现异步过滤,至少能够显示一些“处理..”动画来帮助用户克服这个问题?
我编写了一个用于在ComboBox中过滤项目的代码:
我的问题是,你会怎么做?
我认为这种带反射的解决方案可能非常慢 ..
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += this.FilterPredicate;
private bool FilterPredicate(object value)
{
if (value == null)
return false;
if (String.IsNullOrEmpty(SearchedText))
return true;
int index = value.ToString().IndexOf(
SearchedText,
0,
StringComparison.InvariantCultureIgnoreCase);
if ( index > -1) return true;
return FindInProperties(new string[] { "Property1", "Property2" }, value, SearchedText);
}
private bool FindInProperties(string[] properties, object value, string txtToFind)
{
PropertyInfo info = null;
for (int i = 0; i < properties.Length; i++)
{
info = value.GetType().GetProperty(properties[i]);
if …Run Code Online (Sandbox Code Playgroud) 这就是我想要的:如果我将 ICollectionview 绑定到 DataGrid,我不想丢失 Viewmodel 中的 SortDescription。
\n\n我创建了一个小示例项目来看看我的意思。在我的项目中,我只是使用用户控件在数据网格中显示我的数据。如果我这样做,当 UserControl 卸载时, SortDescription 就会消失,因为 ItemsSource 设置为 null。如果我使用 TemplateSelector 显示我的 UserControl,则 SortDescription不会消失,并且 ItemsSource 在卸载时不会设置为 null。问题是,为什么会有这些不同的行为?这两种行为中的一种是错误吗?
\n\n顺便提一句。我使用 .Net 4.5.1 但安装了 4.6.1 和 system.Windows.Interactivity 4.0.0.0
\n\n主窗口.xaml
\n\n<Window x:Class="DataGridICollectionView.MainWindow"\n xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"\n xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\n xmlns:d="http://schemas.microsoft.com/expression/blend/2008"\n xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"\n xmlns:local="clr-namespace:DataGridICollectionView"\n xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"\n mc:Ignorable="d"\n Title="MainWindow" Height="350" Width="525">\n<Window.Resources>\n <DataTemplate DataType="{x:Type local:ViewmodelListe}">\n <local:MyViewUc/>\n </DataTemplate>\n</Window.Resources>\n<Grid>\n <Grid.RowDefinitions>\n <RowDefinition Height="Auto"/>\n <RowDefinition Height="Auto"/>\n </Grid.RowDefinitions>\n\n <ToolBar Grid.Row="0">\n <Button Content="SetWorkspace MyView" Click="Button_Click"/>\n <Button Content="SetWorkspace Other" Click="Button_Click_1"/>\n </ToolBar>\n\n <ContentPresenter Grid.Row="1" Content="{Binding Workspace}"/>\n</Grid>\n</Window>\nRun Code Online (Sandbox Code Playgroud)\n\nMainWindow.xaml.cs
\n\nnamespace …Run Code Online (Sandbox Code Playgroud) 我有以下绑定到 DataGrid 的 ObservableCollection:
public ObservableCollection<Message> Messages = new ObservableCollection<Message>;
Run Code Online (Sandbox Code Playgroud)
XAML:
<DataGrid ItemsSource="{Binding Path=Messages}">
Run Code Online (Sandbox Code Playgroud)
我在启动时对其进行排序,使用默认视图:
ICollectionView view = CollectionViewSource.GetDefaultView(Messages);
view.SortDescriptions.Add(new SortDescription("TimeSent", ListSortDirection.Descending));
Run Code Online (Sandbox Code Playgroud)
一切正常,但问题是每当我向 Messages 集合添加新消息时,它只会附加到列表的底部,而不是自动排序。
Messages.Add(message);
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?我确信我可以通过每次添加项目时刷新视图来解决这个问题,但这似乎是错误的做法(更不用说性能方面了)。
我在我的视图模型中将 ListView 绑定到 ICollectionView。ICollectionView 有一些预定义的过滤器,当您单击某些按钮时会应用这些过滤器。但是,在过滤集合后,我似乎找不到任何方法来(自动)选择 ListView 中的第一项。
我尝试设置 SelectedIndex=0,将 Target 和 Source 通知添加到绑定中,但是当过滤器应用时所有这些都无效。
关于如何实现这一目标的任何指示?
编辑:下面的代码说明了我要说的问题。
XAML:
<Window x:Class="CollectionViewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CollectionViewTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!-- MENU -->
<StackPanel Orientation="Vertical">
<Button Content="Numbers below 4" Click="Below4_Click" Width="100"/>
<Button Content="Numbers below 7" Click="Below7_Click" Width="100"/>
<Button Content="All numbers" Click="All_Click" Width="100"/>
</StackPanel>
<!-- LIST -->
<ListView
Grid.Column="1"
SelectedIndex="0"
ItemsSource="{Binding Numbers, Mode=OneWay}"
SelectedItem="{Binding SelectedNumber, Mode=TwoWay}">
<ListView.Resources>
<DataTemplate DataType="{x:Type local:Number}">
<TextBlock Text="{Binding …Run Code Online (Sandbox Code Playgroud) 我正在尝试过滤DataGrid中显示的ObservableCollection.这在第一次工作得很好,但是当我尝试再次过滤时,它使用sourcecollection而不是过滤结果.短代码示例:
ICollectionView view = CollectionViewSource.GetDefaultView(myCollection);
view.Filter = delegate(object item){
User user = item as User;
if(user != null && user.Name.ToLower().Contains(textbox.Text.ToLower())) return true;
return false;
};
Run Code Online (Sandbox Code Playgroud)
所以我想要做的只是过滤我的DataGrid中显示的项目,而不是整个集合(当然第一次使用过滤器时,它将使用整个集合).
我已经在WPF中创建了一个listview控件,并具有的成功完全绑定Icollectionview对象ObservableCollection<object>。我的listview列是动态创建的。我必须对列表视图进行排序和分组,但它无法正常工作。我的代码如下。
private void LaodList()
{
dt = new DataTable();
dt.Columns.Add("AA", typeof(string));
dt.Columns.Add("BB", typeof(string));
dt.Columns.Add("cc", typeof(string));
dt.Rows.Add("12", "66",11);
dt.Rows.Add("33", "44",22);
dt.AcceptChanges();
GridView gv = new GridView();
//gv.AllowsColumnReorder = true;
List<string> myItemsCollection = new List<string>();
for (int i = 0; i < dt.Columns.Count; i++)
{
GridViewColumn col = new GridViewColumn();
col.Header = dt.Columns[i].ColumnName;
col.DisplayMemberBinding = new Binding(string.Format("[{0}]", i));
gv.Columns.Add(col);
myItemsCollection.Add(col.Header.ToString());
}
LvItems.View = gv;
this.Source = CollectionViewSource.GetDefaultView(LoadItems(dt)) ;
LvItems.DataContext = this.Source;
cmbGroups.ItemsSource = myItemsCollection;
}
public ObservableCollection<object> LoadItems(DataTable …Run Code Online (Sandbox Code Playgroud) 我绑定ItemsSource的属性DataGrid在我的ViewModel的属性。然后我创建一个ICollectionView并使用DataGrid的项目源创建一个默认视图,如下所示:
_displayItemsView = CollectionViewSource.GetDefaultView(_displayItems);
其中_displayItems是 aList并且_displayItemsView是ICollectionView。
现在我遇到的问题是我允许用户像这样过滤数据网格:
_displayItemsView.Filter = delegate(object item)
{
DISPLAY_ITEM displayItem = (DISPLAY_ITEM)item;
if ((displayItem.RETAIL_ITEM_DPCI.ToString().ToUpper().Contains(value.ToUpper()))
.
.
.
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都很好用,但是如果用户当前正在编辑或DataGrid在ICollectionView. 我应该如何检测:
DataGrid目前正在修改或ICollectionView不在状态,被过滤?感谢任何指导,我对想法持开放态度。谢谢!
icollectionview ×13
wpf ×11
c# ×8
filter ×3
datagrid ×2
.net-3.5 ×1
expression ×1
filtering ×1
iqueryable ×1
listview ×1
mvvm ×1
mvvm-light ×1
performance ×1
predicate ×1
sorting ×1