Posted by: Zeeshan Amjad | June 8, 2010

Event Log Markup Extension


We already created different type of markup extensions. This time we are going to create markup extension for event log. Just like other markup extensions, we inherit one class from MarkupExtension class and then do all the necessary operations. Here is our markup extension class.

[MarkupExtensionReturnType(typeof(EventLogEntry))]
public class EventLogMarkup : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        List<EventLogEntry> eventList = new List<EventLogEntry>();

        EventLog myEventLog = new EventLog("System", ".");

        EventLogEntryCollection myLogEntryCollection = myEventLog.Entries;

        int myCount = myLogEntryCollection.Count;

        for (int i = myCount - 1; i > 0; i--)
        {
            EventLogEntry myLogEntry = myLogEntryCollection[i];
            eventList.Add(myLogEntry);
        }
        return eventList;
    }
}

Here is the usage of this class.

<ListView Margin="6" ItemsSource="{Binding Source={local:EventLogMarkup}}"
		  AlternationCount="2" ItemContainerStyle="{StaticResource alternateColor}">
	<ListView.View>
		<GridView>
			<GridViewColumn Width="Auto" Header="Index" DisplayMemberBinding="{Binding Index}"/>
			<GridViewColumn Width="Auto" Header="Event ID" DisplayMemberBinding="{Binding EventID}"/>
			<GridViewColumn Width="Auto" Header="Source" DisplayMemberBinding="{Binding Source}"/>
			<GridViewColumn Width="Auto" Header="Message" DisplayMemberBinding="{Binding Message}"/>
		</GridView>
	</ListView.View>
</ListView>

We already define the alternate style and namespace local in the XAML. Here is complete code of XAML.

<Window x:Class="WpfEventMarkup.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfEventMarkup"
    Title="Event Log Markup Extension" Height="400" Width="600">
    <Window.Resources>
        <Style x:Key="alternateColor" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="LightBlue"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="AliceBlue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView Margin="6" ItemsSource="{Binding Source={local:EventLogMarkup}}"
                  AlternationCount="2" ItemContainerStyle="{StaticResource alternateColor}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="Auto" Header="Index" DisplayMemberBinding="{Binding Index}"/>
                    <GridViewColumn Width="Auto" Header="Event ID" DisplayMemberBinding="{Binding EventID}"/>
                    <GridViewColumn Width="Auto" Header="Source" DisplayMemberBinding="{Binding Source}"/>
                    <GridViewColumn Width="Auto" Header="Message" DisplayMemberBinding="{Binding Message}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

This is the output of this program.

EventLogMarkupExtension

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: