Bonjour,
Je suis débutant en mvvm et j'essaie de créer un user control respectant ce schéma.
Cependant, même après de longues recherches, je ne comprends toujours pas comment effectuer un binding entre un élément de celui-ci et ma ViewModel.
Le binding réussi dans le constructeur de la VueModel, mais par après, en modifiant la source du binding, plus rien ne se passe :/
Je suppose que j'oublie quelque chose mais je trouve pas quoi
Merci beaucoup pour votre aide
Voici mon code actuel :
Mon UserControl
Code de la Vue MyUserControl : 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 <Window x:Class="TestUserControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestUserControl" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button x:Name="btnShow" Content="Show Text" Command="{Binding ShowCommand}" Grid.Row="0"/> <local:MyUserControl x:Name="myusercontrol" Message="{Binding newMessage}" Grid.Row="1"/> </Grid> </Window>
Code behind de mon user control : 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 public partial class MyUserControl : UserControl { public string Message { get { return (string)GetValue(MessageProperty); } set { SetValue(MessageProperty, value); } } public static readonly DependencyProperty MessageProperty = DependencyProperty.Register( "Message", typeof(string), typeof(MyUserControl), new UIPropertyMetadata(OnMessageChanged)); public static void OnMessageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null) ((MyUserControl)sender).textBlock.Text = e.NewValue.ToString(); } public MyUserControl() { InitializeComponent(); } }
Ma Vue
Code de la fenêtre principale : 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 <Window x:Class="TestUserControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestUserControl" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button x:Name="btnShow" Content="Show Text" Command="{Binding ShowCommand}" Grid.Row="0"/> <local:MyUserControl x:Name="myusercontrol" Message="{Binding newMessage}" Grid.Row="1"/> </Grid> </Window>
Code behind de la fenêtre principale : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public partial class MainWindow : Window { private ViewModel _viewModel; public MainWindow() { InitializeComponent(); _viewModel = new ViewModel(); DataContext = _viewModel; } }
Ma Vue Model
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 public class ViewModel { public string newMessage { get; set; } public ICommand ShowCommand { get; set; } public ViewModel() { newMessage = "test"; ShowCommand = new BaseCommand(ShowMessage, (param) => { return true; }); } private void ShowMessage(object obj) { Console.WriteLine("Je passe par la commande"); newMessage = "TEST"; } }
Partager