Create Binding for ListView in code : ListView « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="WpfApplication1.Monitor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="Monitor" Height="400" Width="389">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key="processes" MethodName="GetProcesses" ObjectType="{x:Type diag:Process}"/>
            <ObjectDataProvider x:Key="dateinfo" ObjectType="{x:Type system:DateTime}"/>
        </Grid.Resources>
        <ListView Name="listView1" ItemsSource="{Binding Source={StaticResource processes}}">
        </ListView>
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Diagnostics;
using System.Windows.Markup;
using System.Xml;
using System.IO;
using System.Timers;

namespace WpfApplication1
{
    public partial class Monitor : Window
    {
        public Monitor()
        {
            InitializeComponent();
            ObjectDataProvider provider = new ObjectDataProvider();
            provider.ObjectType = typeof(Process);
            provider.MethodName = "GetProcesses";

            Binding binding = new Binding();
            PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
            binding.Source = provider;
            binding.Mode = BindingMode.OneWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            listView1.ItemTemplate = CreateItemTemplateFromXaml();
            listView1.SetBinding(ListView.ItemsSourceProperty, binding);
        }

        private DataTemplate CreateItemTemplateFromXaml()
        {
            string templateXml = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><TextBlock Text='{Binding Path=ProcessName}'/></DataTemplate>";
            byte[] templateBytes = Encoding.UTF8.GetBytes(templateXml);

            using (MemoryStream templateStream = new MemoryStream(templateBytes))
            {
                DataTemplate template = (DataTemplate)XamlReader.Load(templateStream);
                return template;
            }
        }

        private DataTemplate CreateItemTemplate()
        {
            DataTemplate template = new DataTemplate();

            FrameworkElementFactory panelFactory = new FrameworkElementFactory(typeof(WrapPanel));

            FrameworkElementFactory idTextFactory = new FrameworkElementFactory(typeof(TextBlock));
            idTextFactory.SetBinding(TextBlock.TextProperty, new Binding("Id"));
            idTextFactory.SetValue(TextBlock.MinWidthProperty, 80d);

            FrameworkElementFactory workingSetTextFactory = new FrameworkElementFactory(typeof(TextBlock));
            workingSetTextFactory.SetBinding(TextBlock.TextProperty, new Binding("WorkingSet"));

            panelFactory.AppendChild(idTextFactory);
            panelFactory.AppendChild(workingSetTextFactory);
            template.VisualTree = panelFactory;

            return template;
        }
    }
}
WPF Create Binding For List View In Code








24.34.ListView
24.34.1.Create a ListView control that implements a GridView view with CheckBox controls for each row.Create a ListView control that implements a GridView view with CheckBox controls for each row.
24.34.2.Use ArrayList as the ListView ItemSourceUse ArrayList as the ListView ItemSource
24.34.3.Create a ListView control that implements a GridView view mode, displays content in groups.Create a ListView control that implements a GridView view mode, displays content in groups.
24.34.4.ListView and ListViewItemListView and ListViewItem
24.34.5.ListView columnsListView columns
24.34.6.Populating ListView rowsPopulating ListView rows
24.34.7.Set Binding ListView.ItemsSourceProperty to ListViewSet Binding ListView.ItemsSourceProperty to ListView
24.34.8.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.
24.34.9.ListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate propertiesListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate properties
24.34.10.Get Bounded item from ListViewGet Bounded item from ListView
24.34.11.Create Binding for ListView in codeCreate Binding for ListView in code
24.34.12.Use three TextBlocks in one ListViewItemUse three TextBlocks in one ListViewItem
24.34.13.Create a ListView control that uses a GridView view mode to display dates.Create a ListView control that uses a GridView view mode to display dates.
24.34.14.Use Path to reference Bounded object in ItemSourceUse Path to reference Bounded object in ItemSource
24.34.15.Enables sorting of data in ascending or descending order according to the contents of one column.Enables sorting of data in ascending or descending order according to the contents of one column.