Précédent   Forum du club des développeurs et IT Pro > Dotnet > Développement Windows > Windows Presentation Foundation
Windows Presentation Foundation Forum d'entraide sur le développement d'applications Windows avec Windows Presentation Foundation
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 16/02/2013, 14h48   #1
Oberown
Membre confirmé
 
Inscription : juillet 2004
Messages : 783
Détails du profil
Informations forums :
Inscription : juillet 2004
Messages : 783
Points : 230
Points : 230
Par défaut Binding d'un ItemsControl avec une list d'enum

Bonjour,

Je bind un ItemsControl avec une List<MonEnum> et je souhaiterai afficher le nom de mon enum.

Comment faire ?

Merci pour votre aide
Oberown est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/02/2013, 01h26   #2
MABROUKI
Membre Expert
 
mebarek
Inscription : avril 2008
Messages : 1 036
Détails du profil
Informations personnelles :
Nom : mebarek
Âge : 53

Informations forums :
Inscription : avril 2008
Messages : 1 036
Points : 1 534
Points : 1 534
bonjour Oberown

ceci pourait repondre au souci:un ObservableCollection.....

code behind .cs du form et de l'enum.....
Code :
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
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.Collections.ObjectModel;
 
namespace WpfEnum
{
    /// <summary>
    /// Logique d'interaction pour Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }
    }
    //le list
    public class ListEnums : ObservableCollection<DaysOfTheWeek>
    {
        public ListEnums()
        {
            // Display the set of legal enum values
            DaysOfTheWeek c = DaysOfTheWeek.Friday;
            DaysOfTheWeek[] o = (DaysOfTheWeek[])Enum.GetValues(c.GetType());
 
            foreach (DaysOfTheWeek day in o)
            {
                this.Add(day);
            }
 
        }
 
 
    }
    // l'enum
    /// The Days of the Week enumeration
    /// </summary>
    public enum DaysOfTheWeek
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    }
}
code xam du winform:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
<Window x:Class="WpfEnum.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfEnum"
        Title="Window3" Height="300" Width="300">
    <Window.Resources>
        <local:ListEnums x:Key="datasource"></local:ListEnums>
    </Window.Resources>
    <ItemsControl
        ItemsSource="{StaticResource datasource}">
    </ItemsControl>
</Window>
bon code.............
MABROUKI est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/02/2013, 10h09   #3
Oberown
Membre confirmé
 
Inscription : juillet 2004
Messages : 783
Détails du profil
Informations forums :
Inscription : juillet 2004
Messages : 783
Points : 230
Points : 230
Merci, mais comment faire pour le template ?

Code WPF :
1
2
3
4
5
6
7
8
<ItemsControl ItemsSource="{StaticResource datasource}">
 <ItemsControl.ItemTemplate>
                <DataTemplate>                    
                        <TextBlock VerticalAlignment="center" Text="??????" />
<!-- Autre morceau de code -->
                 </DataTemplate>
</ItemsControl.ItemTemplate> 
</ItemsControl>
Oberown est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/02/2013, 18h09   #4
sfxzeus
Membre du Club
 
Inscription : juillet 2010
Messages : 52
Détails du profil
Informations forums :
Inscription : juillet 2010
Messages : 52
Points : 46
Points : 46
Bonjour,
il te suffit de faire un binding sur l'objet en cour de parcours par l'itemscontrol
Code xaml :
1
2
3
4
5
6
7
8
 
<ItemsControl ItemsSource="{Binding enums}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" TextAlignment="Center"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Donc il suffit d'écrire {Binding} ou {Binding Path=.}

Tu n'es pas obligé d'utiliser une ObservableCollection si tes données sont fixes.

Si tu souhaites toutes les valeurs de ton enum tu aurais pu simplement écrire aussi
Code c# :
1
2
 
public string[] EnumValues { get { return Enum.GetNames(typeof(EnumTest)); } }
Et binder ton itemsControl à EnumValues

Ou tout en xaml avec ce code trouvé sur StackOverflow
Code xaml :
1
2
3
4
5
6
7
8
9
 
<ObjectDataProvider MethodName="GetValues" 
    ObjectType="{x:Type sys:Enum}" x:Key="odp">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:CompassHeading"/>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
 
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" />
sfxzeus est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/02/2013, 21h35   #5
MABROUKI
Membre Expert
 
mebarek
Inscription : avril 2008
Messages : 1 036
Détails du profil
Informations personnelles :
Nom : mebarek
Âge : 53

Informations forums :
Inscription : avril 2008
Messages : 1 036
Points : 1 534
Points : 1 534
rebonjour

voici comment avec des triggers pour personnaliser eventuellement les differents "enum value":

Code :
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
32
33
34
35
36
37
38
39
40
41
42
 
<Window x:Class="WpfEnum.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfEnum"
        Title="Window3" Height="300" Width="300">
    <Window.Resources>
        <local:ListEnums x:Key="datasource"></local:ListEnums>
        <!--le  DataTemplate--> 
        <DataTemplate 
            x:Key="daystemplate" 
            DataType="{x:Type local:DaysOfTheWeek}">
            <Border 
                x:Name="root" 
                BorderThickness="2" 
                CornerRadius="6" 
                BorderBrush="Pink">
                <TextBlock
                    x:Name="tb"
                    FontSize="18"
                    Foreground="Black"
                    FontFamily="Arial"
                    FontWeight="Normal"
                    Text="{Binding}" 
                    />
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding }" Value="{x:Static local:DaysOfTheWeek.Monday}">
                    <Setter TargetName="tb" Property="FontWeight" Value="Bold" />
                    <Setter TargetName="tb" Property="Foreground" Value="Blue" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ItemsControl
            Margin="8"
            ItemTemplate="{StaticResource daystemplate}"
            ItemsSource="{StaticResource datasource}">
    </ItemsControl>
    </StackPanel>
</Window>
tu peux aussi utiliser un dataprovider .Regarde ce lien de MSDN library....
http://www.google.fr/url?sa=t&rct=j&...42553238,d.Yms
bon soiree..............
MABROUKI est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/02/2013, 00h28   #6
Oberown
Membre confirmé
 
Inscription : juillet 2004
Messages : 783
Détails du profil
Informations forums :
Inscription : juillet 2004
Messages : 783
Points : 230
Points : 230
Le truc que je fais je bind une list de mon enum.
Et dans chacun des items controls j'ai une dropdownlist avec les différents enums. Mais je n'arrive pas à faire un itemselect

Code C# :
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
32
33
34
35
36
37
38
39
40
41
public class EnumToDictionnaryExtension : MarkupExtension
    {
 
        public Type TargetEnum { get; set; }
        public EnumToDictionnaryExtension(Type type) { TargetEnum = type; }
 
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var type = TargetEnum;
            if (type == null) return null;
            return RetrieveFromCacheOrAddIt(type);
        }
 
        private readonly Dictionary<Type, List<Tuple<Object, Object>>> _cache = new Dictionary<Type, List<Tuple<Object, Object>>>();
        private object RetrieveFromCacheOrAddIt(Type type)
        {
            if (!this._cache.ContainsKey(type))
            {
                var fields = type.GetFields().Where(field => field.IsLiteral);
                var values = new List<Tuple<Object, Object>>();
                foreach (var field in fields)
                {
                    var a = (IInfoAttributeInterface[])field.GetCustomAttributes(typeof(IInfoAttributeInterface), false);                    
                    var valueOfField = field.GetValue(type);
                    if (a.Length > 0)
                    {                        
                        var newTuple1 = new Tuple<Object, Object>(valueOfField, a[0].Title);
                        values.Add(newTuple1);
                    }
                    else
                    {
                        var newTuple = new Tuple<Object, Object>(valueOfField, valueOfField);
                        values.Add(newTuple);
                    }
                }
                this._cache[type] = values;
            }
 
            return this._cache[type];
        }
    }

Code WPF :
1
2
3
4
5
6
7
8
9
10
11
12
 <ItemsControl ItemsSource="{Binding Path=ListEnum}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
 
                        <ComboBox DisplayMemberPath="Item2" SelectedValuePath="Item1"
                                SelectedValue="???"
                   ItemsSource="{extension:EnumToDictionnary {x:Type local:MonEnum}}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
Oberown est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 16h02.


 
 
 
 
Partenaires

Hébergement Web