2013-10-15 34 views
0

爲了使用Shift鍵裹面板內選擇多個複選框。(發現在2個點/矩形控制

捕獲上移KEYDOWN鼠標按下事件的位置,在第二鼠標向下與移動keydown,獲得選擇的2個位置,然後需要選擇與選定區域中的複選框控制

如何找到兩個位置(System.Window.Point)或System.Windows.rect 。下面的代碼是選擇wrappanel中的所有複選框(lesscolorpanel)。

private System.Windows.Point startPoint; 
    System.Windows.Point checkpPoint; 
    private System.Windows.Point PointWhereMouseIs; 
    private void LessColourPanel_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) 
    { 
     if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) 
     { 
      if (startPoint == checkpPoint) 
      { 
       //GET THE MOUSE POSITION 
       startPoint = Mouse.GetPosition(lessColourPanel); 
       PointWhereMouseIs = checkpPoint; 
      } 
      else if(PointWhereMouseIs==checkpPoint) 
      { 
       //CAPTURE END MOUSE POSITION 
       PointWhereMouseIs = Mouse.GetPosition(lessColourPanel); 
       //FIND CONTROLS WIHIN RECTANGLE 
       Rect selareaRect = new Rect(startPoint, PointWhereMouseIs); 
       foreach (System.Windows.Controls.CheckBox chkitemBox in FindVisualChildren<System.Windows.Controls.CheckBox>(lessColourPanel)) 
       { 
        var rectBounds = VisualTreeHelper.GetDescendantBounds(chkitemBox); 
        Vector vector = VisualTreeHelper.GetOffset(chkitemBox); 
        rectBounds.Offset(vector); 
        if (rectBounds.IntersectsWith(selareaRect)) 
        { 
         chkitemBox.IsChecked = true; 
        } 
       } 
       startPoint = checkpPoint; 
      } 

     } 
    } 
+0

請勿在WPF中的過程代碼中創建或操作UI元素。這就是XAML的用途。刪除所有這些代碼,併爲此使用'ItemsControl'和適當的ViewModel。 –

+0

@highcore,謝謝你,但它是如何幫助我使用Shift鍵實現多選的,你能給我一個例子嗎? – Jay

+0

我不確定你想要達到什麼目的。默認情況下,「CheckBox」與其他CheckBox沒有任何關係。因此,您將得到多個選擇,而無需按Shift或任何其他鍵。 –

回答

0

得到它,併爲觀衆指Ashley Davis post

的榮譽屬於阿什利·戴維斯。

+0

使用此解決方案時,如果您的控件位於scrollviewer下,則需要重新編寫一些代碼。 – Jay