Tuesday, 4 October 2011

WPF: Binding example (Part 4)


We saw in the last post how we can change the UI of our WPF Task Manager to show all the processes in a grid layout using a ListView control. ListView is useful when we are restricted to .NET 3.5, because in .NET 4.0 framework we have the DataGrid control. There are certain shortcomings in ListView – one being it doesn’t allow automatic sorting of rows. Even if we implement sorting it doesn’t give a visual hint on the column we clicked to sort data. DataGrid is a complete control with all the important features. So let’s modify our application to use the DataGrid control.

We start by removing the ListView tag in XAML and replacing it with:

<DataGrid Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=ProcList}" Background="Gray" Foreground="Black" Name="lst" HorizontalContentAlignment="Stretch">
</DataGrid>

And we get this:

image

Cool! All the columns are automatically generated and the cell type changes based on the data it holds. For e.g. HasExited is a bool so it changes to a check box.

But we do not need all the columns so let’s remove them.

We add this attribute AutoGenerateColumns="False" to the grid tag and add the DataGridTextColumns in the DataGrid.Columns collection.

        <DataGrid Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=ProcList}" Background="Gray" Foreground="Black" Name="lst" HorizontalContentAlignment="Stretch"
                   AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=ProcessName}" />
                <DataGridTextColumn Header="Working Set" Binding="{Binding Path=WorkingSet}"/>
                <DataGridTextColumn Header="Private Memory" Binding="{Binding Path=PrivateMemorySize}"/>
                <DataGridTextColumn Header="Virtual Memory" Binding="{Binding Path=VirtualMemorySize}"/>
            </DataGrid.Columns>
        </DataGrid>
image

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Shorts - week 3, 2022

Post with links to what I am reading: 1. A very good post on different aspects of system architecture: https://lethain.com/introduction-to-a...