Bonjour,
voila j'ai un customcontrol dans lequel je défini le style avec une textbox dedans
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
14
15
16
 
    <Style TargetType="{x:Type local:CustomControlText}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControlText}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBox 
<TextBox Text="{TemplateBinding Text}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
je synchronise le text de la textbox avec un template binding sur la textproperty se trouvant dans le control
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
 
 public class CustomControlText : Control
    {
        static CustomControlText()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControlText), new FrameworkPropertyMetadata(typeof(CustomControlText)));
        }
 
 
        //-------------------------------------------------------------------------------
        #region Text
        //-------------------------------------------------------------------------------
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        //-------------------------------------------------------------------------------
        public static readonly DependencyProperty TextProperty =
                              DependencyProperty.Register(
                              "Text",
                              typeof(string),
                              typeof(CustomControlText),
                              new FrameworkPropertyMetadata(String.Empty, 
                                  FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                  new PropertyChangedCallback(TextChanged),
                                  new CoerceValueCallback(TextCoerce), 
                                  true, 
                                  UpdateSourceTrigger.LostFocus
                                      ));
        //-------------------------------------------------------------------------------
        public static Object TextCoerce(DependencyObject d, Object baseValue)
        {
            CustomControlText _CustomControlText = d as CustomControlText;
            // insert you code here          
 
            return baseValue; // replace this line if necessary
        }
        //-------------------------------------------------------------------------------
        public static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CustomControlText _CustomControlText = d as CustomControlText;
            // déactivated only used for simple type - int, double ... -)
            //string _Text = (string)e.NewValue;
            string _Text = e.NewValue as string;
            // insert you code here
 
        }
        //-------------------------------------------------------------------------------
        #endregion
        //-------------------------------------------------------------------------------
    }
et la si je colle un binding sur le text d'un classe
ca marche bien dans le sens class -> customcontrol
mais des que l'on tape du text on perd le binding ...

si je remplace la ligne
Code : Sélectionner tout - Visualiser dans une fenêtre à part
<TextBox Text="{TemplateBinding Text}" />
par
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControlText}},Path=Text}"/>
ca marche bien

j'aimerais juste comprendre pourquoi ici le templatebinding ne fonctionne pas
et quel est la differente entre ces deux binding

si quelqu'un a une idee ?
Merci