Set up ObjectDataProvider in code : ObjectDataProvider « 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 » ObjectDataProviderScreenshots 
Set up ObjectDataProvider in code
Set up ObjectDataProvider 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.Bind to ObjectDataProviderBind to ObjectDataProvider
2.Bind to Object to ObjectDataProviderBind to Object to ObjectDataProvider
3.The ObjectDataProvider exposes the enum as a binding sourceThe ObjectDataProvider exposes the enum as a binding source
4.Create ObjectDataProvider and bind object to it in codeCreate ObjectDataProvider and bind object to it in code
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.