Bonjour j'essaye de me mettre tout doucement au WPF, je me suis créer une petit UserControl ou je tente de Binder une propriété de ma classe sur le champ Text d'un TextBlock mais je doit mal mi prendre. J'ai réussi à faire fonctionner mon UserControl Uniquement en affectant la propriété DataContext de mon UserControl à this mais je trouve pas ça top.

La partie XAML:

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
 
<UserControl x:Class="WPFConvertorHS.Gestion_Affichage.Outils.UserCtrlEcranAff"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="WhiteSmoke">
        <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border Grid.Row="0" BorderBrush="#FFC3D1DF" BorderThickness="2" CornerRadius="10,10,0,0" Background="#FFC3D1DF" />
        <Border Grid.Row="1" BorderBrush="#FFC3D1DF" BorderThickness="2" CornerRadius="0,0,2,2" />
 
        <TextBlock x:Name="lblTitre" Grid.Row="0" Margin="10,2,2,2" TextAlignment="Left" VerticalAlignment="Center" Text="{Binding ElementName=this, Path=Title}"/>
    </Grid>
</UserControl>
La partie cs :

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
 
  /// <summary>
  /// Logique d'interaction pour UserCtrlEcranAff.xaml
  /// </summary>
  public partial class UserCtrlEcranAff : UserControl, INotifyPropertyChanged
  {
    private string _title;
 
    public string Title 
    {
      get { return _title; }
      set
      {
        if (_title != value)
        {
          _title = value;
          NotifyPropertyChanged("Title");
        }
      }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    public UserCtrlEcranAff()
    {
      InitializeComponent();
      Title = "Titre";
    }
 
    private void NotifyPropertyChanged(string pNom)
    {
      if (PropertyChanged != null)
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(pNom));
      }
    }
 
  }
Et j'ai une deuxième question, (j'ai contourné ou résolu mon premier problème en utilisant une DependencyProperty et avec le DataContext du UC sur lui même).

J'utilise mon UC dans une Window classique, j'ai voulu faire un Binding du Text d'une TextBox sur Title de mon UC. Ca fonctionne au démarrage mais si je modifie le texte de mon Edit le Title de l'UC n'est pas modifié.

Voila la ligne du Binding :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
        <TextBox x:Name="txtBoxTest" Grid.Row="2" Text="{Binding Path=EcranTest.Title, Mode= TwoWay}" />