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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » ListViewScreenshots 
Create Binding for ListView in code
Create Binding for ListView in code
  

<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;
        }
    }
}

   
    
  
Related examples in the same category
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.
2.Use ArrayList as the ListView ItemSourceUse ArrayList as the ListView ItemSource
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.
4.ListView and ListViewItemListView and ListViewItem
5.ListView columnsListView columns
6.Populating ListView rowsPopulating ListView rows
7.Set Binding ListView.ItemsSourceProperty to ListViewSet Binding ListView.ItemsSourceProperty to ListView
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.
9.ListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate propertiesListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate properties
10.Get Bounded item from ListViewGet Bounded item from ListView
11.Use three TextBlocks in one ListViewItemUse three TextBlocks in one ListViewItem
12.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.
13.Use Path to reference Bounded object in ItemSourceUse Path to reference Bounded object in ItemSource
14.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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.