2010-02-08 11 views
9

में डब्ल्यूपीएफ कमांड समर्थन मैं अपने कॉम्बोबॉक्स के चयन में मेरे व्यूमोडेल निष्पादन पर एक कमांड चाहता हूं। स्पष्ट रूप से Combobox निष्पादन आदेशों का समर्थन नहीं करता है।कॉम्बोबॉक्स

मैंने एक नई कक्षा बनाई है जो कॉमबॉक्स से विरासत में है और इस इंटरफ़ेस को लागू करता है।

जब मैं नियंत्रण (डिजाइनर या डिबग में) को देखने की कोशिश करता हूं तो नियंत्रण नहीं दिखाता है। मुझे कोई अपवाद नहीं मिला - क्या मेरा नियंत्रण एक दृश्य टेम्पलेट या कुछ खो रहा है?

धन्यवाद।

public class CommandSourceComboBox : ComboBox, ICommandSource 
{ 
    static CommandSourceComboBox() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(CommandSourceComboBox), new FrameworkPropertyMetadata(typeof(CommandSourceComboBox))); 
    } 

    #region ICommandSource Members 

    public ICommand Command 
    { 
     get; 
     set; 
    } 

    public object CommandParameter 
    { 
     get; 
     set; 
    } 

    public IInputElement CommandTarget 
    { 
     get; 
     set; 
    } 

    protected override void OnSelectionChanged(SelectionChangedEventArgs e) 
    { 
     base.OnSelectionChanged(e); 

     if (this.Command != null) 
     { 
      RoutedCommand command = Command as RoutedCommand; 

      if (command != null) 
      { 
       command.Execute(CommandParameter, CommandTarget); 
      } 
      else 
      { 
       ((ICommand)Command).Execute(CommandParameter); 
      } 
     } 
    } 

    #endregion 
} 

उत्तर

10

सुनिश्चित नहीं है कि यह सही तरीके से क्यों प्रदर्शित नहीं हुआ है। शायद आपको बेस कन्स्ट्रक्टर निष्पादित करने की आवश्यकता है?

संपादित करें, मैं वास्तव में यह परीक्षण किया है और यह इस लाइन लगता है: मेरे लिए

DefaultStyleKeyProperty.OverrideMetadata(typeof(ComboBoxWithCommand), new FrameworkPropertyMetadata(typeof(ComboBoxWithCommand))); 

टूट जाता है यह।

यहाँ मेरी कार्यान्वयन है और यह डिजाइनर में काम करता है:

public class ComboBoxWithCommand : ComboBox, ICommandSource 
{ 
    private static EventHandler canExecuteChangedHandler; 

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", 
                          typeof(ICommand), 
                          typeof(ComboBoxWithCommand), 
                          new PropertyMetadata((ICommand)null, 
                          new PropertyChangedCallback(CommandChanged))); 

    public ICommand Command 
    { 
     get 
     { 
      return (ICommand)GetValue(CommandProperty); 
     } 
     set 
     { 
      SetValue(CommandProperty, value); 
     } 

    } 

    public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", 
                            typeof(IInputElement), 
                            typeof(ComboBoxWithCommand), 
                            new PropertyMetadata((IInputElement)null)); 

    public IInputElement CommandTarget 
    { 
     get 
     { 
      return (IInputElement)GetValue(CommandTargetProperty); 
     } 
     set 
     { 
      SetValue(CommandTargetProperty, value); 
     } 
    } 

    public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", 
                            typeof(object), 
                            typeof(ComboBoxWithCommand), 
                            new PropertyMetadata((object)null)); 

    public object CommandParameter 
    { 
     get 
     { 
      return (object)GetValue(CommandParameterProperty); 
     } 
     set 
     { 
      SetValue(CommandParameterProperty, value); 
     } 
    } 

    public ComboBoxWithCommand() : base() { } 


    private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ComboBoxWithCommand cb = (ComboBoxWithCommand)d; 
     cb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue); 
    } 

    private void HookUpCommand(ICommand oldCommand, ICommand newCommand) 
    { 
     if (oldCommand != null) 
     { 
      RemoveCommand(oldCommand, newCommand); 
     } 
     AddCommand(oldCommand, newCommand); 
    } 

    private void RemoveCommand(ICommand oldCommand, ICommand newCommand) 
    { 
     EventHandler handler = CanExecuteChanged; 
     oldCommand.CanExecuteChanged -= handler; 
    } 

    private void AddCommand(ICommand oldCommand, ICommand newCommand) 
    { 
     EventHandler handler = new EventHandler(CanExecuteChanged); 
     canExecuteChangedHandler = handler; 
     if (newCommand != null) 
     { 
      newCommand.CanExecuteChanged += canExecuteChangedHandler; 
     } 
    } 
    private void CanExecuteChanged(object sender, EventArgs e) 
    { 

     if (this.Command != null) 
     { 
      RoutedCommand command = this.Command as RoutedCommand; 

      // If a RoutedCommand. 
      if (command != null) 
      { 
       if (command.CanExecute(this.CommandParameter, this.CommandTarget)) 
       { 
        this.IsEnabled = true; 
       } 
       else 
       { 
        this.IsEnabled = false; 
       } 
      } 
      // If a not RoutedCommand. 
      else 
      { 
       if (Command.CanExecute(CommandParameter)) 
       { 
        this.IsEnabled = true; 
       } 
       else 
       { 
        this.IsEnabled = false; 
       } 
      } 
     } 
    } 

    protected override void OnSelectionChanged(SelectionChangedEventArgs e) 
    { 
     base.OnSelectionChanged(e); 

     if (this.Command != null) 
     { 
      RoutedCommand command = this.Command as RoutedCommand; 

      if (command != null) 
      { 
       command.Execute(this.CommandParameter, this.CommandTarget); 
      } 
      else 
      { 
       ((ICommand)Command).Execute(CommandParameter); 
      } 
     } 
    } 
} 
+0

यह बहुत अच्छा है! क्या मैं वास्तव में चाहता हूँ। दुर्भाग्यवश अब कॉम्बोबॉक्स पर लागू सभी शैलियों को कॉम्बोबॉक्स विथ कॉमांड ऑब्जेक्ट्स पर लागू नहीं होता है। क्या आपको शैली को कम करने के लिए एक आसान तरीका पता है? या मुझे शैली को डुप्लिकेट करने और कॉम्बोबॉक्स WithCommand प्रकार को लक्षित करने की आवश्यकता है? – KrisTrip

+1

कभी नहीं, मैंने इसे समझ लिया। मैंने अपने स्टाइल टेम्पलेट में निम्न जोड़ा: <स्टाइल एक्स: की = "{x: स्थानीय टाइप करें: कॉम्बोबॉक्स विथकॉमैंड}" टार्गेट टाइप = "{एक्स: स्थानीय टाइप करें: कॉम्बोबॉक्स विथ कॉमांड}" आधारितऑन = "{स्टेटिक रिसोर्स {एक्स: कॉम्बोबॉक्स टाइप करें}}" /> – KrisTrip

+0

कोड भी मेरे लिए काम किया, पोस्टिंग के लिए धन्यवाद! – dain