Bonjour, je n'arrive à afficher les composants en mode design lorsque leurs propriétés Visibility sont bindées.
Voici un exemple de code simpliste pour expliquer mon problème :
Classe VM qui est le DataContext de la form
Le XAML de la form :
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 public class MiniVM : INotifyPropertyChanged { public MiniVM() { _IsVisibleParDefaut = true; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private bool _IsVisibleParDefaut; public bool IsVisibleParDefaut { get { return _IsVisibleParDefaut; } set { if (value != _IsVisibleParDefaut) { _IsVisibleParDefaut = value; OnPropertyChanged("IsVisibleParDefaut"); OnPropertyChanged("IsInvisibleParDefaut"); } } } public bool IsInvisibleParDefaut { get { return !_IsVisibleParDefaut; } } }
Et l'événement du bouton :
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 <Window x:Class="LaboWPF.BacASable" 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:laboWpf="clr-namespace:LaboWPF" Title="BacASable" Height="300" Width="300" mc:Ignorable="d" d:DataContext="{d:DesignInstance laboWpf:MiniVM}"> <Window.Resources> <BooleanToVisibilityConverter x:Key="BoolToVisibility" /> </Window.Resources> <UniformGrid> <Label Content="Visible" Visibility="{Binding IsVisibleParDefaut, Converter={StaticResource BoolToVisibility}}"/> <Label Content="InVisible" Visibility="{Binding IsInvisibleParDefaut, Converter={StaticResource BoolToVisibility}}" /> <Button Content="Changer la visibilité" Click="ButtonBase_OnClick"/> </UniformGrid> </Window>
En fait, ce problème n'arrive que lorsque je définit quel est le DataContext de la form dans le code xaml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { (DataContext as MiniVM).IsVisibleParDefaut = !(DataContext as MiniVM).IsVisibleParDefaut; }
Mais je souhaite garder ce code car il est plus facile de s'y retrouver dans les gros codes xaml et aussi pour la complétion de code de resharper.
Code : Sélectionner tout - Visualiser dans une fenêtre à part d:DataContext="{d:DesignInstance laboWpf:MiniVM}"
J'ai essayé en utilisant d:LayoutOverrides mais ça ne fonctionne pas, ainsi qu'en essayant d'utiliser d: DesignData.
Merci par avance pour votre aide.
Partager