IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

[CONVERTER] binding une resource externe


Sujet :

Windows Presentation Foundation

  1. #1
    Membre expérimenté
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Points : 1 313
    Points
    1 313
    Par défaut [CONVERTER] binding une resource externe
    bonjour

    j'aimerais savoir si quelqu'un a deja fait cela
    je crée un composant a qui on peut passer un converter (IValueConverter)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     public class ValueConverterControl : Control
        {
            static ValueConverterControl()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(ValueConverterControl), new FrameworkPropertyMetadata(typeof(ValueConverterControl)));
            }
     
            #region ValueConverter (DP SHORT)
            public IValueConverter ValueConverter { get { return (IValueConverter)GetValue(ValueConverterProperty); } set { SetValue(ValueConverterProperty, value); } }
            public static readonly DependencyProperty ValueConverterProperty = DependencyProperty.Register("ValueConverter", typeof(IValueConverter), typeof(ValueConverterControl), new PropertyMetadata(null));
            #endregion 
        }
    j'utilise un converter exemple
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     public class SampleConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    que j'utilise ainsi
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <DockPanel.Resources>
                <SampleConverter x:Key="sc" />            
            </DockPanel.Resources>
      <ValueConverterControl Background="Red"
                                   Height="25"
                                   DockPanel.Dock="Top"
                                   ValueConverter="{StaticResource sc}" />
    j'aimerais savoir comment binder un textbox par exemlple dans la definition de mon objet pour que ca marche
    comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     <Style TargetType="{x:Type comp:ValueConverterControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type comp:ValueConverterControl}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <TextBlock Text="{Binding ., Converter={TemplateBinding ValueConverter}}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    mais la ca marche pas
    j'ai tenté ces 3 syntax sans resultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    <TextBlock Text="{Binding ., Converter={TemplateBinding ValueConverter}}" />
    <TextBlock Text="{Binding ., Converter={Binding ValueConverter}}" />
    <TextBlock Text="{Binding ., Converter={Binding ValueConverter, RelativeSource={RelativeSource TemplateParent}}}"/>
    si quelqu'un a deja fait cela ?
    IKEAS : Finalement je crois que c'est dans ses faiblesses que l'on y trouve a la fois de la force et a la fois de la richesse...
    ----------------------------------------------------
    Si vous avez du taf en wpf & design d'application sympa, contactez moi !!!!
    http://ultimatecorp.eu/wpf/

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour
    TemplateBindingExtension est selon MSDN lib un MarkupExtension.
    extrait :
    public class TemplateBindingExtension : MarkupExtension
    Propriétés

    Nom Description
    Converter Obtient ou définit le convertisseur qui interprète entre la source et la cible d'une liaison.
    ConverterParameter Obtient ou définit le paramètre à passer au convertisseur.
    Property Obtient ou définit la propriété qui est liée.

    definition de la prop Converter :
    C#
    public IValueConverter Converter { get; set; }

    Donc la prop Converter n'est pas un DP et un Binding ou TemplateBinding lève un exception...
    La seule solution c'est de definir "un binding par code dans OnApplyRemplate()" de ton control et d'assigner:
    ton DP ValueConverter au Binding...
    ton code revu:
    1/le Class SampleConverter
    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
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    public class SampleConverter : IValueConverter
        {
           
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string str = value as string;
                if  (String.IsNullOrEmpty(str))
                { 
                    return 0;
                }
                else
                {
                    int n = 0;
    
    
                    for (int i = 0; i < str.Length; i++)
                    {
                        n +=System.Convert.ToInt32(str[i]);
                    }
                    return n;
                }
            }
    
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
    
    
    2/le Class ValueConverterControl avec son nécessaire attribut TemplatePart
    
    
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
     [TemplatePart(Name = "PART_tb", Type = typeof(TextBlock))]
        public class ValueConverterControl : Control
        {
            private TextBlock currentTb;
            public ValueConverterControl()
            {
    
    
            }
            static ValueConverterControl()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(ValueConverterControl),
                    new FrameworkPropertyMetadata(typeof(ValueConverterControl)));
    
    
            }
            public IValueConverter  ValueConverter
            {
                get { return (IValueConverter)GetValue(ValueConverterProperty); }
                set { SetValue(ValueConverterProperty, value); }
            }
    
    
            // Using a DependencyProperty as the backing store for ValueConverter.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ValueConverterProperty =
                DependencyProperty.Register("ValueConverter",
                typeof(IValueConverter),
                 typeof(ValueConverterControl),
                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
          
    
    
            public string TheText
            {
                get { return (string)GetValue(TheTextProperty); }
                set { SetValue(TheTextProperty, value); }
            }
    
    
            // Using a DependencyProperty as the backing store for TheText.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty TheTextProperty =
                DependencyProperty.Register("TheText",
                typeof(string),
                typeof(ValueConverterControl),
                new FrameworkPropertyMetadata(string.Empty,
                    FrameworkPropertyMetadataOptions.AffectsRender,
                    OnTheTextChanged));
            private static void OnTheTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
            {
                ValueConverterControl ctl = d as ValueConverterControl;
                string  txt = (string)args.NewValue;
    
    
                if (ctl != null)
                {
                    ctl.InvalidateVisual();
                }
    
    
            }
    
    
    
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                if (this.Template != null )
                {
                        TextBlock tb = this.Template.FindName("PART_tb", this) as TextBlock;
                        if (tb !=null)
                        {
                                currentTb = tb;
                               if (this.TheText !=null )
                               {
                                    Binding myBinding = new Binding();
                                    myBinding.Path =new PropertyPath( "TheText");
                                    myBinding.Converter = this.ValueConverter;
                                    myBinding.Source = this;
                          
                                    currentTb.SetBinding(TextBlock.TextProperty, myBinding);
    
    
                                }
                        }  
                }
            }
                
        }
    2/ son Generic.xaml
    <Style TargetType="{x:Type local:ValueConverterControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ValueConverterControl}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock x:Name="PART_tb" Width="{TemplateBinding local:ValueConverterControl.Width}" Height="{TemplateBinding local:ValueConverterControl.Width}" Foreground="{TemplateBinding local:ValueConverterControl.Foreground}" FontSize="{TemplateBinding local:ValueConverterControl.FontSize}" TextAlignment="Left" Text="MOMO"> </TextBlock> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
    4/code XAML du Form User:
    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
     
    Window x:Class="WpfMultiBindingConverter.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfMultiBindingConverter" 
            Title="MainWindow" Height="350" Width="525">
     
        <StackPanel >
            <StackPanel.Resources>
                <local:SampleConverterBis x:Key="MyConverter"/>
            </StackPanel.Resources>
            <local:ValueConverterControl
     
                BorderBrush="Blue" 
               BorderThickness="2.0"
                Foreground="Red" 
                FontSize="36"
                ValueConverter="{StaticResource MyConverter}"
     
                TheText="Allo">
     
            </local:ValueConverterControl>
        </StackPanel >
    </Window>
    Bon code...

  3. #3
    Membre expérimenté
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    1 562
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 562
    Points : 1 313
    Points
    1 313
    Par défaut
    oui j'ai fait un truc dans ce style
    sans dp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    public IValueConverter ItemConverter { get; set; } = null;
    avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <EnumFilterControl DockPanel.Dock="Top"
              Type="{x:Type ei:StatMachine}"
              ItemConverter="{StaticResource smconv}"
    />
    et ca marche bien
    IKEAS : Finalement je crois que c'est dans ses faiblesses que l'on y trouve a la fois de la force et a la fois de la richesse...
    ----------------------------------------------------
    Si vous avez du taf en wpf & design d'application sympa, contactez moi !!!!
    http://ultimatecorp.eu/wpf/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [CR9] faire une Jointure externe
    Par coldec dans le forum SAP Crystal Reports
    Réponses: 5
    Dernier message: 28/06/2005, 12h10
  2. Valeur de retour d'une procédure externe
    Par elekis dans le forum x86 32-bits / 64-bits
    Réponses: 4
    Dernier message: 16/04/2004, 16h45
  3. [Builder] Effectuer une opération quand une resource a changé
    Par Satch dans le forum Eclipse Platform
    Réponses: 2
    Dernier message: 08/04/2004, 13h22
  4. Creer et utiliser une librairie externe à l'exécutable
    Par multani dans le forum Autres éditeurs
    Réponses: 7
    Dernier message: 06/11/2003, 10h11
  5. convertion d'une chaine binaire
    Par Mister dans le forum C
    Réponses: 3
    Dernier message: 03/10/2003, 22h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo