J'ai l'application suivante avec une barre d'outils, une liste de maîtres et une vue détaillée:
Le détail est "injecté" via ContentControl. Le détail contient un UserControl, qui contient un ScrollViewer et ainsi de suite. À un moment donné, il y a un "ZoomPanControl" (pas le mien) qui fournit une commande "FitView".
Je souhaite exécuter la commande "FitView" depuis ma barre d'outils pour la vue détaillée actuellement active.
Le bouton de ma barre d'outils ressemble à ceci:
<fluent:Button x:Name="FitButton" Command="{Binding ?WhatGoesHere?}" Header="Fit View"/>
Je ne peux pas comprendre comment lier la propriété de commande du bouton de la barre d'outils au ZoomPanControl actuellement actif. Je ne "vois" même pas ce contrôle lors de la liaison de commande.
Any hint how this problem is generally solved is highly appreciated.
Here's how I solved the problem. Luckily I have access to the source code of the ZoomPanControl.
First I have implemented a DependencyProperty in the ZoomPanControl for the "FitView" command like this:
public static readonly DependencyProperty FitCommandDepPropProperty = DependencyProperty.Register(
"FitCommandDepProp", typeof (ICommand), typeof (ZoomAndPanControl), new PropertyMetadata(default(ICommand)));
public ICommand FitCommandDepProp
{
get { return (ICommand) GetValue(FitCommandDepPropProperty); }
set { SetValue(FitCommandDepPropProperty, value); }
}
In the "OnApplyTemplate()" method of the control I set the dependency property:
FitCommandDepProp = FitCommand;
In the detail-View of my application I bind the command-dependency-property to my ViewModel like this:
<zoomAndPan:ZoomAndPanControl x:Name="zoomAndPanControl"
FitCommandDepProp="{Binding FitCommand, Mode=OneWayToSource}"
The important part is Mode=OneWayToSource. This "forwards" the command from the ZoomPanControl to my detail-viewmodel.
Detail-viewmodel a la propriété de ICommand à laquelle se lier. À partir de ce moment, j'ai la commande dans ma logique viewmodel. J'ai implémenté un mécanisme pour passer le FitCommand au viewmodel qui est lié à la barre d'outils. Vous pouvez utiliser un événement ou ce que vous préférez pour passer la commande.
Le modèle de vue de la barre d'outils a à nouveau une propriété ICommand pour FitCommand.
public ICommand FitCommand
{
get { return _fitCommand; }
set
{
if (Equals(value, _fitCommand)) return;
_fitCommand = value;
NotifyOfPropertyChange(() => FitCommand);
}
}
Dans la vue de la barre d'outils, je me lie simplement à cette propriété:
<fluent:Button x:Name="FitButton" Command="{Binding FitCommand}" Header="Fit View"/>
Après cela, les commandes de vue sont disponibles pour chaque vue de détail séparément.
Mais je n'ai aucune idée de comment résoudre ce problème sans accéder au code source de ZoomPanControl.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots