Bonjour,
un truc assez étrange : pour référencer la fenêtre courante dans un binding il devrait être possible de la nommer via "x:Name" et de la référencer via "ElementName";
mais ce n'est apparemment pas le cas dans cet exemple :
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 <Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Wpf.Window1" x:Name="Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:Wpf" Title="Window1" Width="100" Height="150"> <StackPanel> <Border BorderBrush="Blue" BorderThickness="1"> <Label Content="{Binding ElementName=Control,Path=Test}" Height="25" /> </Border> <Border BorderBrush="Blue" BorderThickness="1"> <Label Content="{Binding ElementName=Window,Path=Test}" Height="25" /> </Border> <Border BorderBrush="Blue" BorderThickness="1"> <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=Test}" Height="25" /> </Border> <local:UserControl1 x:Name="Control"/> </StackPanel> </Window>Il est en revanche possible d'y accéder via "RelativeSource" comme le montre l'exemple précédent.
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 using System.Windows; namespace Wpf { public partial class Window1 : Window { public Window1() { InitializeComponent(); Test = "123"; } public string Test { get; set; } } }
De même le binding via "ElementName" fonctionne parfaitement avec le "UserControl1" :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <UserControl xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Wpf.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
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 using System.Windows.Controls; namespace Wpf { public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); Test = "456"; } public string Test { get; set; } } }
Il doit manquer un détail, mais lequel ?
Merci.
Partager