简体   繁体   English

c#如何修改ListView

[英]c# How to modify a ListView

I have a GUI application that has a ListView. 我有一个具有ListView的GUI应用程序。 It is used to show the log of the app. 它用于显示应用程序的日志。 In the xaml I have the following: 在xaml中,我具有以下内容:

<ListView x:Name="lvStatus" Margin="5,5,5,5" ItemsSource="{Binding LogView}"
                          ItemTemplate="{StaticResource StatusListTemplate}">
                </ListView>

In the code the listView is initialized and used with a ListCollectionView: 在代码中,listView被初始化并与ListCollectionView一起使用:

public ListCollectionView LogView {get; private set; }
 ...
ObservableCollectionLog uiLogSink = new ObservableCollectionLog();
                Logger.RegisterLogSink(uiLogSink);
                LogView = new ListCollectionView(uiLogSink.Entries);

I would like at some point clear the ListView. 我想在某个时候清除ListView。 I can't just run a ListView.Clear. 我不能只运行ListView.Clear。

Any idea how I can control my ListView? 知道如何控制ListView吗?

Thanks Tony 谢谢托尼

只需清除绑定的数据源即可。

You can create a CollectionView wrapping the uiLogSynk and bind the listView to the CollectionView: 您可以创建一个包装uiLogSynk的CollectionView并将listView绑定到CollectionView:

_view = new ListCollectionView(uiLogSynk); _view =新的ListCollectionView(uiLogSynk);

Whenever you click the "Clear" log button you record the length of your uiLogSynk. 每当您单击“清除”日志按钮时,都会记录uiLogSynk的长度。

int startDisplayLogIndex = 0; int startDisplayLogIndex = 0;

public void buttonClick(...) { startDisplayLogIndex = uiLogSynk.Length; public void buttonClick(...){startDisplayLogIndex = uiLogSynk.Length; } }

all you have to do is to attach a filter to the _view and specify a filter function that compares the index of each element. 您要做的就是在_view上附加一个过滤器,并指定一个比较每个元素索引的过滤器函数。

_view.Filter = new Predicate(ShouldDisplayLog); _view.Filter = new谓词(ShouldDisplayLog);

public bool IsValueTruck(Object value) { return (uiLogSynk.IndexOf(value) >= startDisplayLogIndex); 公共布尔IsValueTruck(对象值){返回(uiLogSynk.IndexOf(value)> = startDisplayLogIndex); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM