Bonjour à tous,
Je rencontre actuellement un petit problème mais je ne vois pas pourquoi!!!
Une fenêtre avec 1 un usercontrol auquel je voudrais faire passer une valeur.
Ma fenêtre
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <Window x:Class="Test.Application.GUI.Test2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:Test.Application.GUI.Test2" Title="Window1" Height="300" Width="300"> <Grid> <StackPanel> <TextBox Text="{Binding Path=OneStringValue}" /> // Affiche bien Ceci est un test <uc:Uc1 OneString="{Binding Path=OneStringValue}" /> // Erreur de binding 'OneStringValue' property not found (voir plus bas) </StackPanel> </Grid> </Window>
Code C# : 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 namespace Test.Application.GUI.Test2 { public partial class Window1 : Window { public struct StructContext{ public string OneStringValue { get; set; } } public Window1() { InitializeComponent(); DataContext = new StructContext(){OneStringValue = "Ceci est un test"}; } } }
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <UserControl x:Class="Test.Application.GUI.Test2.Uc1" 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"> <Grid> <TextBox Text="{Binding Mode=OneWay}" /> </Grid> </UserControl>
Mon UserControl
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <UserControl x:Class="Test.Application.GUI.Test2.Uc1" 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"> <Grid> <TextBox Text="{Binding Mode=OneWay}" /> </Grid> </UserControl>
Code C# : 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 namespace Test.Application.GUI.Test2 { public partial class Uc1 : UserControl { public string OneString{ get{ return (string)GetValue(OneStringProperty); } set{ SetValue(OneStringProperty, value); } } public static readonly DependencyProperty OneStringProperty = DependencyProperty.Register("OneString", typeof(string), typeof(Uc1), new PropertyMetadata("Not value for string", new PropertyChangedCallback(TextProperty_Changed))); public Uc1() { InitializeComponent(); DataContext = OneString; } private static void TextProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((Uc1) d).DataContext = e.NewValue.ToString(); } } }
Et voici la fameuse erreur que j'ai
Je en comprend pas pourquoi juste au dessus ça fonctionne et pour le userControl ça ne fonctionne plus.System.Windows.Data Error: 39 : BindingExpression path error: 'OneStringValue' property not found on 'object' ''String' (HashCode=-140625727)'. BindingExpressionath=OneStringValue; DataItem='String' (HashCode=-140625727); target element is 'Uc1' (Name=''); target property is 'OneString' (type 'String')
Merci d'avance
Partager