I having a WPF application in which the UI has a list box. The list box has binding of ObservableCollection. Log class implements INotifyPropertyChanged.
The list will show the continuous logging of the application. As long as the application is running. The ObservableCollection size keeps on growing. After some time I get the Out of Memory exception. I want to show the latest 1000 entries in the list control. Any suggestions on this will be of great help!!
XAML:
<DataGrid AutoGenerateColumns="False" SelectedValue="{Binding SelectedLog}" SelectionUnit="FullRow" SelectionMode="Single" Name="dataGridLogs"
ItemsSource="{Binding Path=LogList}" CanUserReorderColumns="True" CanUserResizeRows="True" CanUserDeleteRows="False" IsReadOnly="True"
CanUserAddRows="False" EnableColumnVirtualization="True" EnableRowVirtualization="True" SelectionChanged="grid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Time Stamp" Binding="{Binding StrTimeStamp, Mode=OneWay}" Width="Auto"/>
<DataGridTextColumn Header="Action" Binding="{Binding Action, Mode=OneWay}" Width="Auto"/>
</DataGrid>
ViewModel:
public ObservableCollection<LogData> LogList
{
get
{
if (logList == null)
{
logList = new ObservableCollection<LogData>();
}
return logList;
}
set
{
logList = value;
OnPropertyChanged("LogList");
}
}
model:
public class LogData : INotifyPropertyChanged
{
public LogData()
{
}
private String timestamp = string.Empty;
public String StrTimestamp
{
get
{
if (timestamp == null)
return string.Empty;
return timestamp ;
}
set
{
timestamp = value;
}
}
public string Action
{
get;set;
}
}
Net-C (also known as NetCourrier) is a French webmail service created and run by Mail Object, a company based in Saint-Maur-des-Fossés, in Val-de-Marne.
You can write . NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language. F# is a programming language that makes it easy to write succinct, robust, and performant code.
it can be easily done by this class:
public class LimitedSizeObservableCollection<T> : ObservableCollection<T>
{
public int Capacity { get; }
public LimitedSizeObservableCollection(int capacity)
{
Capacity = capacity;
}
public new void Add(T item)
{
if (Count >= Capacity)
{
this.RemoveAt(0);
}
base.Add(item);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With