简体   繁体   中英

c# How to modify a ListView

I have a GUI application that has a ListView. It is used to show the log of the app. In the xaml I have the following:

<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:

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. I can't just run a ListView.Clear.

Any idea how I can control my ListView?

Thanks Tony

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

You can create a CollectionView wrapping the uiLogSynk and bind the listView to the CollectionView:

_view = new ListCollectionView(uiLogSynk);

Whenever you click the "Clear" log button you record the length of your uiLogSynk.

int startDisplayLogIndex = 0;

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.Filter = new Predicate(ShouldDisplayLog);

public bool IsValueTruck(Object value) { return (uiLogSynk.IndexOf(value) >= startDisplayLogIndex); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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