2010-08-05 14 views
48

में दर्ज करने पर viewmodels कमांड निष्पादित करना जब मैं टेक्स्टबॉक्स में प्रवेश करता हूं तो मैं अपने व्यूमोडेल में एक कमांड निष्पादित करना चाहता हूं। कमांड बटन पर बाध्य होने पर काम करता है।टेक्स्टबॉक्स

<Button Content="Add" Command="{Binding Path=AddCommand}" /> 

लेकिन मैं इसे टेक्स्टबॉक्स से काम पर नहीं ला सकता हूं। मैंने इनपुट इनपुट की कोशिश की, लेकिन यह काम नहीं किया।

<TextBox.InputBindings> 
    <KeyBinding Command="{Binding Path=AddCommand}" Key="Enter"/> 
</TextBox.InputBindings> 

मैं भी डिफ़ॉल्ट के रूप में काम कर रहे बटन सेट करने की कोशिश की, लेकिन जब दर्ज दबाया जाता है यह निष्पादित नहीं होती है।

आपकी मदद के लिए धन्यवाद।

+1

आप कृपया स्वीकार किए जाते हैं उत्तर को बदल सकते हैं? स्वीकार किए जाने पर मैं अपनाई हटा नहीं सकता। – Jay

उत्तर

0

मैं इसे हटाने के लिए वर्षों से प्रयास कर रहा हूं, लेकिन एक स्वीकृत उत्तर हटाया नहीं जा सकता है। https://stackoverflow.com/a/21110210/114994 देखें।

+5

अगर हमें कोड-कोड में कोड लिखना है तो क्यों न केवल कुंजीडाउन ईवेंट में कमांड को कॉल करें? – thewpfguy

+1

क्योंकि इस दृष्टिकोण के साथ आप अभी भी अपने व्यूमोडेल – Wouter

+4

से जुड़ सकते हैं यह XAML के माध्यम से उपलब्ध नहीं होने पर कार्यक्षमता को विस्तारित करने का एक अच्छा उदाहरण है, और यह काम करता है। लेकिन इस सवाल का सही जवाब एक @mkamioner प्रदान किया गया है। –

5

यहां एक संलग्न निर्भरता संपत्ति है जिसे मैंने बनाया है। यह सुनिश्चित करने का लाभ है कि आपके पाठ बाइंडिंग को कमांड आग से पहले व्यूमोडेल पर अपडेट किया गया है (चांदी की रोशनी के लिए उपयोगी जो संपत्ति का समर्थन नहीं करता है अद्यतन स्रोत ट्रिगर बदल गया है)।

public static class EnterKeyHelpers 
{ 
    public static ICommand GetEnterKeyCommand(DependencyObject target) 
    { 
     return (ICommand)target.GetValue(EnterKeyCommandProperty); 
    } 

    public static void SetEnterKeyCommand(DependencyObject target, ICommand value) 
    { 
     target.SetValue(EnterKeyCommandProperty, value); 
    } 

    public static readonly DependencyProperty EnterKeyCommandProperty = 
     DependencyProperty.RegisterAttached(
      "EnterKeyCommand", 
      typeof(ICommand), 
      typeof(EnterKeyHelpers), 
      new PropertyMetadata(null, OnEnterKeyCommandChanged)); 

    static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) 
    { 
     ICommand command = (ICommand)e.NewValue; 
     FrameworkElement fe = (FrameworkElement)target; 
     Control control = (Control)target; 
     control.KeyDown += (s, args) => 
     { 
      if (args.Key == Key.Enter) 
      { 
       // make sure the textbox binding updates its source first 
       BindingExpression b = control.GetBindingExpression(TextBox.TextProperty); 
       if (b != null) 
       { 
        b.UpdateSource(); 
       } 
       command.Execute(null); 
      } 
     }; 
    } 
} 

आप इस तरह इसका इस्तेमाल:

<TextBox 
    Text="{Binding Answer, Mode=TwoWay}" 
    my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"/> 
13

आप शायद आदेश एक संपत्ति नहीं किया है, लेकिन एक क्षेत्र। यह केवल गुणों से बांधने के लिए काम करता है। अपनी AddCommand को किसी संपत्ति में बदलें और यह काम करेगा। (आपका एक्सएएमएल कमांड के लिए फ़ील्ड के बजाय मेरे लिए ठीक काम करता है -> किसी भी कोड के लिए कोई ज़रूरत नहीं है!)

+2

सहमत हुए। '<कीबाइंडिंग कमांड =" {बाध्यकारी पथ = AddCommand} "कुंजी =" एंटर "/> 'मेरे लिए भी बहुत अच्छा काम किया! –

123

मुझे पता है कि मैं पार्टी के लिए देर हो चुकी हूं, लेकिन मुझे यह मेरे लिए काम करने के लिए मिला। Key="Enter"

यहाँ के बजाय Key="Return" उपयोग करने का प्रयास पूर्ण उदाहरण है

<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}"> 
    <TextBox.InputBindings> 
     <KeyBinding Command="{Binding AddCommand}" Key="Return" /> 
    </TextBox.InputBindings> 
</TextBox> 

अपने बंधन में UpdateSourceTrigger=PropertyChanged का उपयोग सुनिश्चित करें जब तक ध्यान केंद्रित खो दिया है अन्यथा संपत्ति अपडेट नहीं किया जाएगा बनाओ, और अहम फोकस खो देंगे दर्ज ...

आशा है कि यह सहायक होगा!

+22

इसे भयानक 'रजिस्टर एटैचड' के साथ स्वीकार्य उत्तर के बजाय समाधान के रूप में स्वीकार किया जाना चाहिए – monstr

2

आप की बाइंडिंग की मुख्य संपत्ति के बजाय इशारा परिभाषित करने की जरूरत:

<TextBox.InputBindings> 
    <KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/> 
</TextBox.InputBindings> 
0

इसके अलावा Mark Heath करने के जवाब है, मैं इस तरह से कमान पैरामीटर संलग्न संपत्ति को लागू करने से एक कदम आगे वर्ग लिया;

public static class EnterKeyHelpers 
{ 
     public static ICommand GetEnterKeyCommand(DependencyObject target) 
     { 
      return (ICommand)target.GetValue(EnterKeyCommandProperty); 
     } 

     public static void SetEnterKeyCommand(DependencyObject target, ICommand value) 
     { 
      target.SetValue(EnterKeyCommandProperty, value); 
     } 

     public static readonly DependencyProperty EnterKeyCommandProperty = 
      DependencyProperty.RegisterAttached(
       "EnterKeyCommand", 
       typeof(ICommand), 
       typeof(EnterKeyHelpers), 
       new PropertyMetadata(null, OnEnterKeyCommandChanged)); 


     public static object GetEnterKeyCommandParam(DependencyObject target) 
     { 
      return (object)target.GetValue(EnterKeyCommandParamProperty); 
     } 

     public static void SetEnterKeyCommandParam(DependencyObject target, object value) 
     { 
      target.SetValue(EnterKeyCommandParamProperty, value); 
     } 

     public static readonly DependencyProperty EnterKeyCommandParamProperty = 
      DependencyProperty.RegisterAttached(
       "EnterKeyCommandParam", 
       typeof(object), 
       typeof(EnterKeyHelpers), 
       new PropertyMetadata(null)); 

     static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) 
     { 
      ICommand command = (ICommand)e.NewValue; 
      Control control = (Control)target; 
      control.KeyDown += (s, args) => 
      { 
       if (args.Key == Key.Enter) 
       { 
        // make sure the textbox binding updates its source first 
        BindingExpression b = control.GetBindingExpression(TextBox.TextProperty); 
        if (b != null) 
        { 
         b.UpdateSource(); 
        } 
        object commandParameter = GetEnterKeyCommandParam(target); 
        command.Execute(commandParameter); 
       } 
      }; 
     } 
    } 

उपयोग:

<TextBox Text="{Binding Answer, Mode=TwoWay}" 
    my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}" 
    my:EnterKeyHelpers.EnterKeyCommandParam="your parameter"/> 
संबंधित मुद्दे