Bonjour,
Débutant en WPF,
J'ai un usercontrol B contenant deux radio boutons. J'aimerai utiliser ce usercontrol dans un usercontrol A.
Le souci, c'est que j'aimerai binder B avec un view model instancier dans le constructeur du view model de A. C'est la que j'ai mon erreur.
View B
Code View A
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 <UserControl x:Class="TestModeView.View.Common.ModeView" 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" xmlns:common="clr-namespace:TestModeView.ViewModel.Common" mc:Ignorable="d" d:DesignHeight="50" d:DesignWidth="300" Background="{StaticResource MainBackground}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.Resources> <common:EnumBooleanConverter x:Key="enumBooleanConverter"/> </Grid.Resources> <RadioButton GroupName="SimulatorMode" Grid.Column="0" HorizontalAlignment="Center" Content="Mode 1" Margin="{StaticResource ModeMargin}" IsChecked="{Binding Path=Mode, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=0}" IsEnabled="{Binding Path=IsEnable, UpdateSourceTrigger=PropertyChanged}" /> <RadioButton GroupName="SimulatorMode" Grid.Column="1" HorizontalAlignment="Center" Content="Mode 2" Margin="{StaticResource ModeMargin}" IsChecked="{Binding Path=Mode, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=1}" IsEnabled="{Binding Path=IsEnable, UpdateSourceTrigger=PropertyChanged}" /> </Grid> </UserControl>
Construteur du view model A
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 <UserControl x:Class="TestModeView.View.ServerView" 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" xmlns:View="clr-namespace:TestModeView.View.Common" mc:Ignorable="d" d:DesignWidth="300" Background="{StaticResource MainBackground}"> <!-- xmlns:my="clr-namespace:TestModeView.ViewModel.Common" Background="{StaticResource MainBackground}"> --> <!-- Control border --> <Border BorderThickness="1" BorderBrush="{StaticResource BorderColor}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <!-- <Grid.Resources> <my:EnumBooleanConverter x:Key="enumBooleanConverter"/> </Grid.Resources> --> <!-- HEADER --> <Border Grid.Row="0" Background="{StaticResource HeaderBackground}"> <Label HorizontalAlignment="Center" Content="Server" FontSize="14" FontFamily="Georgia" FontWeight="Bold"/> </Border> <!-- Mode --> <Grid Grid.Row="1"> <View:ModeView x:Name="ModeView" /> </Grid> </Grid> </Border> </UserControl>
C'est le "ModeView.DataContext = modeViewModel;" qui émet une erreur. J'aimerai dire à l'user control, de se binder avec le view modele que je viens d'instancier
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 public ServerViewModel(CommunicationManager com) { if (com == null) throw new NullReferenceException("com"); //Initialize the communication manager _com = com; IsEnable = true; //* ModeViewModel modeViewModel = new ModeViewModel(2, _com); ModeView.DataContext = modeViewModel; //*/ }
Partager