请建议一种最简单的方法,从具有'N'项的集合中获取计数'n'的随机混洗集合.其中n <= N
函数具有object类型的参数.我调试它以查看程序的其他部分发送给我的数据类型.基础是ObservableCollection.我如何将其转换为列表?
我正在使用一个ObservableCollection带有两个ICollectionView不同的过滤器.
一种用于按某种类型过滤消息,一种用于计算已检查的消息.正如您所看到的,消息过滤器和消息计数工作正常,但是当我取消检查时,消息将从列表中消失(计数仍然有效).
对于这篇长篇文章,我很抱歉,我想包括所有相关内容.
XAML代码:
<!-- Messages List -->
<DockPanel Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
Height="500">
<ListBox Name="listBoxZone"
ItemsSource="{Binding filteredMessageList}"
Background="Transparent"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone"
Content="{Binding text}"
Tag="{Binding id}"
Unchecked="CheckBoxZone_Unchecked"
Foreground="WhiteSmoke"
Margin="0,5,0,0"
IsChecked="{Binding isChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
<Button Content="Test Add New"
Grid.Column="2"
Height="25"
HorizontalAlignment="Left"
Margin="34,2,0,0"
Click="button1_Click" />
<Label Content="{Binding checkedMessageList.Count}"
Grid.Column="2"
Height="25"
Margin="147,2,373,0"
Width="20"
Foreground="white" />
Run Code Online (Sandbox Code Playgroud)
截图:

码:
/* ViewModel Class */
public class MainViewModel : INotifyPropertyChanged
{
// Constructor
public MainViewModel()
{
#region …Run Code Online (Sandbox Code Playgroud) c# wpf observablecollection collectionviewsource icollectionview
我有ObservableCollection<T>集合,我想用新的元素集合替换所有元素,我可以这样做:
collection.Clear();
Run Code Online (Sandbox Code Playgroud)
要么:
collection.ClearItems();
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,这两种方法有什么区别?)
我也foreach可以collection.Add一个接一个地使用,但这会多次发射
添加元素集合时也一样.
编辑:
我在这里找到了一个很好的库:增强的ObservableCollection能够延迟或禁用通知,但它似乎不支持silverlight.
我在这里坚持这个:
ObservableCollection<Employee> list = new ObservableCollection<Employee>();
dgEmployees.ItemsSource = list;
Run Code Online (Sandbox Code Playgroud)
当您调试列表变量时,它是空的(list.Count = 0),但是然后我将它绑定到DataGrid(WPFToolkit),它向我显示一个空行.
在即时窗口中,对于dgEmployees.Items,它显示:
dgEmployees.Items[0]
{NewItemPlaceholder}
Run Code Online (Sandbox Code Playgroud)
和
dgEmployees.Items[0].GetType()
{Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
[System.RuntimeType]: {Name = "NamedObject" FullName = "MS.Internal.NamedObject"}
Run Code Online (Sandbox Code Playgroud)
这似乎发生在我将这个Datagrid放入TabControl后,但我不确定它与它有什么关系.
有谁知道如何删除这个空白行?
我在这个链接上找到了
ObservableCollection没有注意到它中的Item何时发生变化(即使使用INotifyPropertyChanged)
一些通知Observablecollection项目已更改的技术.这个链接中的TrulyObservableCollection似乎正是我正在寻找的.
public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
: base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a); …Run Code Online (Sandbox Code Playgroud) c# collections wpf observablecollection inotifypropertychanged
我想在包含对象列表(来自数据库)的视图模型上公开属性.
我需要这个集合是只读的.也就是说,我想阻止添加/删除等.但是允许foreach和indexers工作.我的目的是声明一个包含可编辑集合的私有字段,并使用只读公共属性引用它.如下
public ObservableCollection<foo> CollectionOfFoo {
get {
return _CollectionOfFoo;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,该语法只会阻止更改对集合的引用.它不会阻止添加/删除等
完成此任务的正确方法是什么?
我编写了以下类来实现(或尝试!)带有通知的字典:
public partial class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged
{
public ObservableDictionary() : base() { }
public ObservableDictionary(int capacity) : base(capacity) { }
public ObservableDictionary(IEqualityComparer<TKey> comparer) : base(comparer) { }
public ObservableDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }
public event NotifyCollectionChangedEventHandler CollectionChanged;
public new TValue this[TKey key]
{
get
{
return base[key];
}
set
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, key, …Run Code Online (Sandbox Code Playgroud) 我正在使用BackgroundWorker更新,ObservableCollection但它给出了这个错误:
"这种类型
CollectionView不支持SourceCollection从与Dispatcher线程不同的线程对其进行更改 ."
用最少的工作量来解决这个问题的最佳和最优雅的方法是什么.我不想写低级别的基于锁的多线程代码.
我在网上看到了一些解决方案,但它们已有几年的历史了,所以不确定解决这个问题的最新共识是什么.
.net c# parallel-processing multithreading observablecollection
我有一个绑定到ObservableCollection的WPF ListBox,当集合发生变化时,所有项目都会更新它们的位置.
新位置存储在集合中,但UI不会更新.所以我添加了以下内容:
void scenarioItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ToolboxListItem.UpdatePositions();
lstScenario.ItemsSource = null;
lstScenario.ItemsSource = ToolboxListItem.ScenarioItems;
this.lstScenario.SelectedIndex = e.NewStartingIndex;
}
Run Code Online (Sandbox Code Playgroud)
通过将ItemsSource设置为null然后再次绑定它,UI将更新,
但这可能是非常糟糕的编码:p
建议?
c# ×8
wpf ×4
.net ×3
collections ×1
data-binding ×1
datagrid ×1
dictionary ×1
ienumerable ×1
linq ×1
listbox ×1
silverlight ×1
wpftoolkit ×1