Create Binding for ListView in code

image_pdfimage_print


   
  

<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=&#039;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#039; xmlns:x=&#039;http://schemas.microsoft.com/winfx/2006/xaml&#039;><TextBlock Text=&#039;{Binding Path=ProcessName}&#039;/></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;
        }
    }
}