0

MinValueカスタム コントロールで、タイプのカスタム Dependency Property が呼び出されるシナリオがありintます。

ユーザーがキーボード入力 Ctrl++ (増加) および Ctrl+- (減少) を介してその値を変更できるようにしたいと考えています。これはコマンドで実行できることは理解していますが、それらのコマンドをどこに実装すればよいかわかりません。ユーザーは、ウィンドウ内のどのコントロールにフォーカスがあっても、前述のキーボード ショートカットを使用できる必要があります。

そのコントロールだけがキーボード ショートカットの操作方法を知っているため、カスタム コントロールに Command を実装する必要があると思いますが、カスタム コントロールでいっぱいのリスト ボックスがあり、それぞれがショートカットを処理する必要があるため、方法がわかりません。それはうまくいくでしょう。ポインタはありますか?いつもありがとう!

4

1 に答える 1

0

これを行うには他の方法があり、おそらくこの方法で行わない正当な理由がありますが、ユーザー コントロールにコマンドを配置する実装を次に示します。

ユーザー コントロール コード (簡単にルックレス コントロールになる可能性があります):

public partial class TestControl : UserControl
    {
        //Declare routed commands
        public static RoutedCommand IncrementCommand;
        public static RoutedCommand DecrementCommand;

        static TestControl()
        {
            //Create routed commands and add key gestures.
            IncrementCommand = new RoutedCommand();
            DecrementCommand = new RoutedCommand();
            IncrementCommand.InputGestures.Add(new KeyGesture(Key.Add, ModifierKeys.Control));
            DecrementCommand.InputGestures.Add(new KeyGesture(Key.Subtract, ModifierKeys.Control));
        }

        public TestControl()
        {
            //Subscribe to Increment/Decrement events.
            TestControl.Decremented += down;
            TestControl.Incremented += up;

            InitializeComponent();
        }

        //Declare *static* Increment/Decrement events.
        public static event EventHandler Incremented;
        public static event EventHandler Decremented;

        //Raises Increment event
        public static void IncrementMin(object o, ExecutedRoutedEventArgs args)
        {
            if (Incremented != null)
            {
                Incremented(o, args);
            }
        }

        //Raises Decrement event
        public static void DecrementMin(object o, ExecutedRoutedEventArgs args)
        {
            if (Decremented != null)
            {
                Decremented(o, args);
            }
        }

        //Handler for static increment
        private void down(object o, EventArgs args)
        {
            Min--;
        }

        //Handler for static decrement
        private void up(object o, EventArgs args)
        {
            Min++;
        }

        //Backing property
        public int Min
        {
            get { return (int)GetValue(MinProperty); }
            set { SetValue(MinProperty, value); }
        }

        public static readonly DependencyProperty MinProperty =
            DependencyProperty.Register("Min", typeof(int),
            typeof(TestControl), new UIPropertyMetadata(0));
    }

この動作が必要なウィンドウは、これらのコマンドの CommandBinding を追加する必要があります。ウィンドウ コード ビハインド:

public MainWindow()
{
    InitializeComponent();

    //Raise control's static events in response to routed commands
    this.CommandBindings.Add(
        new CommandBinding(TestControl.IncrementCommand, new ExecutedRoutedEventHandler(TestControl.IncrementMin)));
    this.CommandBindings.Add(
        new CommandBinding(TestControl.DecrementCommand, new ExecutedRoutedEventHandler(TestControl.DecrementMin)));            
}

それは役に立ちますか?

(ああ、ところで-書かれているように、このコードはキーパッドの +/- キーにのみ応答し、 (P)([)(]) キーの上のキーには応答しません。これまでキージェスチャを使用したことがありません。確かに可能です修正する必要はあまりありません。)

于 2013-11-10T00:53:50.277 に答える