passando la finestra corrente come CommandParameter
-
29-09-2019 - |
Domanda
come posso passare la finestra Sono attualmente in come parametro ad un comando?
mi piace fare questo in XAML-markup:
<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
Soluzione
Ci sono due modi che posso di pensare di fare questo: Dare un nome alla finestra (tramite un attributo x:Name
sul tag Window
, e poi costruire un legame come questo (assume il nome della finestra è 'ThisWindow'):
<Button Command="CommandGetsCalled" CommandParameter="{Binding ElementName=ThisWindow}" />
Per qualcosa di più generale (non si basa sul dare la finestra corrente un nome), il legame può essere costruita in questo modo:
<Button Command="CommandGetsCalled" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
Altri suggerimenti
Si potrebbe provare il legame con un RelativeSource
Se si vuole passare il pulsante come parametro:
<Button Command="CommandGetsCalled"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
Se si desidera passare alla finestra come parametro:
<Button Command="CommandGetsCalled"
CommandParameter="{Binding RelativeSource={
RelativeSource AncestorType={x:Type Window}}}" />
Nella mia situazione nessuna delle risposte fornite funzionato.
Questo ha funzionato per me:
<window x:Name="myWindow">
<Button Command="Command" CommandParameter={x:Reference Name=myWindow}/>
</window>