Pour base, j'ai créé une mini classe c# qui sera ma source de données.
using System.Collections.Generic; namespace Tests1 { public class Bidule { public IEnumerableGetList() { return GetList("Titi"); } public IEnumerable GetList(string nom) { List col = new List (); for (int i = 0; i < m_occurence; ++i) { System.Threading.Thread.Sleep(200); col.Add(nom); } return col; } public Bidule() { m_occurence = 2; } public Bidule(int occ) { m_occurence = occ; } private int m_occurence; } }
Instanciation d'une classe sans constructeur et appel d'une méthode sans paramètres
<Window x:Class="Tests1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Tests1" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Tests1" Height="300" Width="300" > <Window.Resources> <ObjectDataProvider x:Key="dataSource" MethodName="GetList" ObjectType="{x:Type local:Bidule}"/> </Window.Resources> <ListBox ItemsSource="{Binding Source={StaticResource dataSource}}"/> </Window>
Instanciation d'une classe avec paramètres dans le constructeur et appel d'une méthode sans paramètres
<Window x:Class="Tests1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Tests1" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Tests1" Height="300" Width="300" > <Window.Resources> <ObjectDataProvider x:Key="dataSource" MethodName="GetList" ObjectType="{x:Type local:Bidule}"> <ObjectDataProvider.ConstructorParameters> <sys:Int32>5</sys:Int32> </ObjectDataProvider.ConstructorParameters> </ObjectDataProvider> </Window.Resources> <ListBox ItemsSource="{Binding Source={StaticResource dataSource}}"/> </Window>
Instanciation d'une classe avec paramètres dans le constructeur et appel d'une méthode avec paramètres
<Window x:Class="Tests1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Tests1" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Tests1" Height="300" Width="300" > <Window.Resources> <ObjectDataProvider x:Key="dataSource" MethodName="GetList" ObjectType="{x:Type local:Bidule}"> <ObjectDataProvider.ConstructorParameters> <sys:Int32>5</sys:Int32> </ObjectDataProvider.ConstructorParameters> <ObjectDataProvider.MethodParameters> <sys:String>Tibi</sys:String> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <ListBox ItemsSource="{Binding Source={StaticResource dataSource}}"/> </Window>
Assignation d'un objet au DataObjectProvider lancement d'une méthode sans paramètres
- code XAML<Window x:Class="Tests1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Tests1" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Tests1" Height="300" Width="300" > <Window.Resources> <ObjectDataProvider x:Key="dataSource" MethodName="GetList"/> </Window.Resources> <ListBox ItemsSource="{Binding Source={StaticResource dataSource}}"/> </Window>- code c#
public partial class Window1 : System.Windows.Window { public Window1() { InitializeComponent(); ObjectDataProvider m_dataSource = FindResource("dataSource") as ObjectDataProvider; m_dataSource.ObjectInstance = new Bidule(30); } }
Quelques informations supplémentaires ;-)
- Il est possible de lancer le chargement de l'objectDataProvider en asynchrone en settant l'attribut IsAsynchronous à "True".
- Par défaut, le remplissage de la propriété Data de l'objectDataProvider se fait au premier appel. Il est possible de l'en empêcher en mettant l'attribut IsInitialLoadEnabled à "False". Il faudra lancer la méthode Refresh() pour positionner la propriété Data.
une réaction
1 De gates - 26/02/2008, 09:41
ton blog st vraiment un petit bijoux soit,dit au passage ;)