poshgui.com

  • Home
  • Pricing
  • Documentation
    • Features | Roadmap
    • Changelog
    • Docs
    • Video Tutorials
  • Contact
  • About
  • Login
  • Home
  • Pricing
  • Documentation
    • Features | Roadmap
    • Changelog
    • Docs
    • Video Tutorials
  • Contact
  • About
  • Login

Docs

In-depth how-to’s on PoshGUI features

Popular Search textboxbuttonimagecombopicture

WPF

  • Starting New Project
  • Adding controls
  • Changing properties
  • Layout
  • Adding Events and Logic
  • DataContext
  • Exporting your project
  • Multithreading
  • Multipage Applications
  • Custom Lists
  • Controls
    • Tab Control
    • Grid
    • Border
    • StackPanel
    • ProgressBar

WinForms

  • Exporting your project
  • Starting New project
  • Adding controls
  • Changing Properties
  • Adding Events and logic

General

  • Cross-platform support
  • Code editors Keyboard Shortcuts
  • Why the UI on the site looks different than in runtime
  • Privacy Policy
  • Terms & Conditions
View Categories
  • Home
  • Docs
  • WPF
  • Custom Lists

Custom Lists

This section combines the understanding of DataContext, Layouts, and Events to create a custom list.

We will first gain the basic understanding of custom lists and then create one advanced level ListView based on custom view.

(Feel free to jump to the section of your choice, directly by clicking on it)

Preparing an item template #

To understand this concept, let us create one small UI that will serve as an item template in our custom list by following the steps below:

Drag the Border from the TOOLBOX onto the WPF form.

Add a StackPanel. Right-click on it and select Fill Parent from the drop-down.

Setup the Orientation option of StackPanel from the PROPERTIES as Horizontal.

Add an Ellipse from TOOLBOX into the StackPanel.

Drop another StackPanel in the existing structure.

Add two TextBlock(s) in the second StackPanel.

Set up Margins and your item template is ready!

Creating a custom list with item template #

After preparing the template for each item, let us create a complex custom list, based on the template of the previous section.

The steps are as follows:

From the TOOLBOX, add a ListView.

Navigate to the DataContext to create an array of objects named ‘data’. You can create complex objects in an array by writing their properties within the curly brackets.

You can assign the properties of your choice along with their values to each of the objects. We have created three properties for an object named ‘name’, ‘lastChecked’, ‘color’.

Here is what the DataContext will look like after assigning properties and values to all four objects in the array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"data": [
{
"name":"poshgui.com",
"lastChecked":"2 minutes ago",
"color":"Green"
},{
"name":"facebook.com",
"lastChecked":"4 minutes ago",
"color":"Red"
},{
"name":"twitter.com",
"lastChecked":"6 minutes ago",
"color":"Green"
},{
"name":"linkedIn.com",
"lastChecked":"8 minutes ago",
"color":"Red"
}]}
{ "data": [ { "name":"poshgui.com", "lastChecked":"2 minutes ago", "color":"Green" },{ "name":"facebook.com", "lastChecked":"4 minutes ago", "color":"Red" },{ "name":"twitter.com", "lastChecked":"6 minutes ago", "color":"Green" },{ "name":"linkedIn.com", "lastChecked":"8 minutes ago", "color":"Red" }]}
{
    "data": [
        {
            "name":"poshgui.com",
            "lastChecked":"2 minutes ago",
            "color":"Green"
        },{
            "name":"facebook.com",
            "lastChecked":"4 minutes ago",
            "color":"Red"
        },{
            "name":"twitter.com",
            "lastChecked":"6 minutes ago",
            "color":"Green"
        },{
            "name":"linkedIn.com",
            "lastChecked":"8 minutes ago",
            "color":"Red"
        }]}

Navigate to the XAML of ListView, and add the

<ListView.ItemTemplate><DataTemplate></DataTemplate></ListView.ItemTemplate>
<ListView.ItemTemplate><DataTemplate></DataTemplate></ListView.ItemTemplate> to create a template of custom list within the ListView.

Add all the controls you added in the Border while working on Custom Lists between the <DataTemplate></DataTemplate> in the previous section. You can refer to steps 2-7 of the preparing item template section.

Select the ListView, and bind it to the ‘data’. This will create a view like follows:

Now, time to bind the respective properties of DataContext to the list items. For that you can add Fill= “{Binding color}” to Ellipse, Text = “{Binding name}” to the first TextBlock, and Text= “{Binding lastChecked}” to the last TextBlock.

After executing, you can see the created custom list as follows:

Code for creating example complex CustomList:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400">
<Grid>
<ListView HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Height="280" VerticalAlignment="Top" Width="327" Margin="266,31,0,0" ItemsSource="{Binding data}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5,5,5,5">
<Ellipse HorizontalAlignment="Left" VerticalAlignment="Top" Fill="{Binding color}" Stroke="Black" Height="50" Width="50"/>
<StackPanel HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Margin="0,0,0,0">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding name}" Margin="10,0,0,0"/>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding lastChecked}" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400"> <Grid> <ListView HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Height="280" VerticalAlignment="Top" Width="327" Margin="266,31,0,0" ItemsSource="{Binding data}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="5,5,5,5"> <Ellipse HorizontalAlignment="Left" VerticalAlignment="Top" Fill="{Binding color}" Stroke="Black" Height="50" Width="50"/> <StackPanel HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Margin="0,0,0,0"> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding name}" Margin="10,0,0,0"/> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding lastChecked}" Margin="10,0,0,0"/> </StackPanel> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Window>
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400">
<Grid>
<ListView HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Height="280" VerticalAlignment="Top" Width="327" Margin="266,31,0,0" ItemsSource="{Binding data}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="5,5,5,5">
                <Ellipse HorizontalAlignment="Left" VerticalAlignment="Top" Fill="{Binding color}" Stroke="Black" Height="50" Width="50"/>
                <StackPanel HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Margin="0,0,0,0">
                    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding name}" Margin="10,0,0,0"/>
                    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding lastChecked}" Margin="10,0,0,0"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
</Grid>
</Window>

Still stuck? How can we help?

How can we help?

Updated on February 21, 2022
Multipage Applications
Table of Contents
  • Preparing an item template
  • Creating a custom list with item template
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT