J'ai une fenêtre popup, dans laquelle j'ai un textblock que je voudrais lier à une propriété dans mon ViewModel. Je l'ai déjà binded avec succès un booléen dans ma fenêtre, et je l'ai fait essentiellement la même chose pour ma chaîne, mais en quelque sorte la propriété de chaîne ne met pas à jour ...Propriété WPF Liaison de TextBlock
Voici mon XAML:
<Popup Margin ="10" HorizontalAlignment="Center" VerticalAlignment="Top" AllowsTransparency="True" IsOpen="{Binding OpenPopup}" Height="150" Width="300">
<Grid Background="#FFFFCCCC">
<TextBlock x:Name="NewVersionText" Margin="10,10,10,10" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="14" Width="230">
Eine neue Version der Applikation ist verfügbar. <LineBreak /> Möchten Sie diese herunterladen?
</TextBlock>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10,10,10,10" TextWrapping="Wrap" Width="230" Text="{Binding DownloadText}"/>
</Grid>
</Popup>
[EDIT]: le changement des propriétés a lieu en cliquant sur ces boutons: celui qui ne
<Button Content="Ja" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="70" Command="{Binding DownloadVersionCommand}"/>
<Button Content="Später" HorizontalAlignment="Left" Height="20" Margin="75,0,0,0" VerticalAlignment="Top" Width="70" Command="{Binding ClosePopupCommand}"/>
la propriété Je binded est avec succès OuvrePopup dans IsOpen = « {Binding OuvrePopup} », Le travail est DownloadText dans Text = "{Binding DownloadText}". Le .xaml a un ViewModel qui est déjà câblé (comme je l'ai dit, cela fonctionne bien avec toutes les autres propriétés).
Le code C# dans mon ViewModel est: [Edit: Les deux propriétés sont dans le même ViewModel] Pour le texte String:
private string _downloadText;
public string DownloadText {
get {
return _downloadText;
}
set {
_downloadText = value;
Debug.WriteLine("DownloadText = " + value);
RaisePropertyChanged();
}
}
private void DownloadVersion() {
DownloadText = "Download gestartet";
VersionManager.downloadFile();
Pour le Popup booléenne:
private bool _openPopup;
public bool OpenPopup {
get {
return _openPopup;
}
set {
_openPopup = value;
Debug.WriteLine("Open Popup = " + value);
RaisePropertyChanged();
}
}
private void ClosePopoup() {
OpenPopup = false;
}
la méthode RaisePropertyChanged() est mise en oeuvre comme suit:
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine("MainWindowViewModel, PropertyChanged: " + propertyName);
}
}
Lorsque le CloseP La méthode opup() est appelée, la propriété change ce qui fait que la propriété IsOpen de ma fenêtre popup passe à false et se ferme comme elle le devrait.
Lorsque la méthode DownloadVersion() est appelée, la propriété DownloadText est également modifiée avec succès, mais pas mise à jour dans ma vue. Des suggestions de ce qui me manque?
[EDIT]: La liaison des boutons:
public ICommand DownloadVersionCommand {
get; set;
}
// In the ViewModel Constructor:
DownloadVersionCommand = new RelayCommand(o => {
Debug.Write("DownloadVersionCommand " + o);
DownloadVersion();
})
Lorsque vous exécutez le programme et affichez le menu contextuel, voyez-vous des erreurs de liaison dans la fenêtre de sortie de Visual Studio? –
Votre code fonctionne parfaitement pour moi si 'DownloadText' et' OpenPopup' sont tous deux dans le même viewmodel. –
Oui, les deux sont dans le même viewmodel ... Quant à la question d'Eric: Je reçois ceci de la fenêtre de sortie: 'App.exe' (CLR v4.0.30319: App.exe): Loaded 'C: \ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Numerics.Vectors \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Numerics.Vectors.dll '. Impossible de trouver ou d'ouvrir le fichier PDB. DownloadVersionCommand DownloadText = Télécharger gestartet MainWindowViewModel, PropertyChanged: DownloadText – scalderon