سؤال

كيف يمكنني تمرير النافذة التي أنا حاليًا كمعلمة إلى أمر؟

أحب القيام بذلك في Xaml-Markup:

<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
هل كانت مفيدة؟

المحلول

هناك طريقتان يمكنني التفكير في القيام بذلك: امنح النافذة اسمًا (عبر أ x:Name ميزة على Window العلامة ، ثم قم ببناء ملزمة مثل هذا (يفترض أن اسم النافذة هو "thiswindow"):

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

لشيء أكثر عمومية (لا يعتمد على إعطاء اسم النافذة الحالية) ، يمكن بناء الربط مثل هذا:

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

نصائح أخرى

يمكنك محاولة الارتباط بالأقارب

إذا كنت تريد تمرير الزر كمعلمة:

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

إذا كنت تريد تمرير النافذة كمعلمة:

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

في وضعي لم تعمل أي من الإجابات المقدمة.

هذا عمل بالنسبة لي:

<window x:Name="myWindow">
 <Button Command="Command" CommandParameter={x:Reference Name=myWindow}/>
</window>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top