c# How to modify a ListView
I have a GUI application t开发者_开发问答hat 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); }
Simply clear your bound data source.
精彩评论