J'ai défini une listbox personnalisée de la mnière suivante:
Bon, ca n'a pas trop d'intérêt présenté comme ca, mais c'est pour réduire le problème à son plus simple niveau.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <UserControl x:Class="TestBinding.CustomListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ListBox x:Name="InnerList" /> </UserControl>
Dans le code-behind j'ai ceci:
J'ai également créer une classe contenant mes données de cette manière:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 using System.Windows.Controls; using System.Collections; namespace TestBinding { public partial class CustomListBox : UserControl { public CustomListBox() { InitializeComponent(); } public IEnumerable ItemsSource { get { return InnerList.ItemsSource; } set { InnerList.ItemsSource = value; } } } }
Et dans la page qui utilise le usercontrol CustomListBox, j'ai ceci:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 using System.Collections.ObjectModel; using System.ComponentModel; using System.Collections.Generic; namespace TestBinding { public class Employee { public string Nom { get; set; } public int Age { get; set; } public override string ToString() { return string.Format("{0} is {1} years old.", Nom, Age); } } public class EmployeesVM: INotifyPropertyChanged { public ObservableCollection<Employee> Employees { get; set; } public EmployeesVM() { Employees = new ObservableCollection<Employee>(); Employees.Add(new Employee() { Nom = "Toto", Age = 27 }); Employees.Add(new Employee() { Nom = "Tata", Age = 35 }); } public event PropertyChangedEventHandler PropertyChanged; } }
Lorsque j'éxécute mon projet, j'obtiens cette exception sur le InitializeComponent de la page principale:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <UserControl x:Class="TestBinding.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300" xmlns:o="clr-namespace:TestBinding"> <UserControl.Resources> <o:EmployeesVM x:Key="MyViewModel" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource MyViewModel}"> <StackPanel> <o:CustomListBox ItemsSource="{Binding Employees}" /> </StackPanel> </Grid> </UserControl>
AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 10 Position: 42]
Je planche depuis quelque temps sur cette erreur, et je ne trouve pas de manière convenable pour faire mon binding sur mon custom contrôle.
Merci
===================
Blog : Silverlight News







Répondre avec citation


Partager