Question

comment puis-je passer la fenêtre, je suis actuellement en tant que paramètre à une commande?

J'aime le faire en XAML balisage:

<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
Était-ce utile?

La solution

Il y a deux façons que je peux de penser à le faire: Donnez la fenêtre un nom (via un attribut x:Name sur l'étiquette Window, puis construire une fixation comme celui-ci (prend le nom de la fenêtre est « ThisWindow »):

<Button Command="CommandGetsCalled" CommandParameter="{Binding ElementName=ThisWindow}" />

Pour quelque chose de plus général (ne vous fiez pas à donner la fenêtre actuelle un nom), la liaison peut être construit comme ceci:

<Button Command="CommandGetsCalled" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" /> 

Autres conseils

Vous pouvez essayer de se lier à un RelativeSource

Si vous voulez passer le bouton comme paramètre:

<Button Command="CommandGetsCalled" 
        CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />

Si vous voulez passer la fenêtre comme paramètre:

<Button Command="CommandGetsCalled" 
        CommandParameter="{Binding RelativeSource={
             RelativeSource AncestorType={x:Type Window}}}" />

Dans ma situation aucune des réponses fournies a travaillé.

Cela a fonctionné pour moi:

<window x:Name="myWindow">
 <Button Command="Command" CommandParameter={x:Reference Name=myWindow}/>
</window>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top