WPF:Input and Commands輸入和命令(3)

CursorType光標類型

clipboard.png

一、實現效果app

  1. 設置鼠標樣式在全局還某一區域
  2. 自定義鼠標樣式

二、關注詞ide

  1. Mouse.OverrideCursor
  2. new Cursor()

三、實現流程
自定義鼠標加載:this

// Setting CustomCursor to the CustomCursor.cur file.
// This assumes the file CustomCursor.cur has been added to the project
// as a resource.  One way to accomplish this to add the following 
// ItemGroup section to the project file
//
//  <ItemGroup>
//    <Content Include="CustomCursor.cur">
//       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
//    </Content>
//  </ItemGroup>
_customCursor = new Cursor(Directory.GetCurrentDirectory() +
                           @"\" +
                           "CustomCursor.cur");

根據ComboBoxItem的項選擇對應鼠標樣式
設置顯示區域.Cursor值,同時更新是否全局鼠標樣式spa

case "WaitCursor":
    DisplayArea.Cursor = Cursors.Wait;
    break;
case "Custom":
    DisplayArea.Cursor = _customCursor;
    break;
 }

// If the cursor scope is set to the entire application
// Use OverrideCursor to force the cursor for all elements
if (_cursorScopeElementOnly == false)
{
    Mouse.OverrideCursor = DisplayArea.Cursor;
}

鼠標顯示區域切換:code

  1. 若Mouse.OverrideCursor = null,則默認全局應用設定樣式
  2. 如Mouse.OverrideCursor = DisplayArea.Cursor;則指定元素重寫鼠標樣式
private void CursorScopeSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        if (source.Name == "rbScopeElement")
        {
            // Setting the element only scope flag to true
            _cursorScopeElementOnly = true;

            // Clearing out the OverrideCursor.  
            Mouse.OverrideCursor = null;
        }
        if (source.Name == "rbScopeApplication")
        {
            // Setting the element only scope flag to false
            _cursorScopeElementOnly = false;

            // Forcing the cursor for all elements. 
            Mouse.OverrideCursor = DisplayArea.Cursor;
        }
    }
}

ProgramaticFocusControl操做控件焦點程序

clipboard.png

一、實現效果:orm

  1. 控件焦點只在指定區域開始,經過方向鍵、Tab移動控件焦點,同時設置焦點時控件顏色改變。
  2. 更加導航方向改變下一個角度元素的屬性

二、關鍵詞:blog

  1. Keyboard.Focus+Keyboard.FocusedElement
  2. FocusNavigationDirection+TraversalRequest

三、靜態組織:
界面不需焦點區域(左側)設置UIElment.Focusable=false
設置觸發器實現:有焦點,則背景色改變。
四、實現流程:
加載時設置指定元素角度開始事件

private void OnLoaded(object sender, RoutedEventArgs e)
{
    // Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton);
}

焦點方向改變時(RadioButton事件):ip

  1. 轉換string值爲焦點導航方向 枚舉值
private void OnFocusSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        _focusMoveValue = (FocusNavigationDirection) Enum.Parse(
            typeof (FocusNavigationDirection), (string) source.Content);
    }
}

尋找將要應用下一個焦點的元素,並修改此元素屬性ci

  1. 獲取當前焦點元素Keyboard.FocusedElement
  2. 獲取當前元素的下一個焦點元素(方向導航的)UIElement.PredictFocus(方向)。
  3. 若爲Control元素,改變其屬性
private void OnPredictFocus(object sender, RoutedEventArgs e)
{
    DependencyObject predictionElement = null;

    var elementWithFocus = Keyboard.FocusedElement as UIElement;

    if (elementWithFocus != null)
    {
        // Only these four directions are currently supported
        // by PredictFocus, so we need to filter on these only.
        if ((_focusMoveValue == FocusNavigationDirection.Up) ||
            (_focusMoveValue == FocusNavigationDirection.Down) ||
            (_focusMoveValue == FocusNavigationDirection.Left) ||
            (_focusMoveValue == FocusNavigationDirection.Right))
        {
            // Get the element which would receive focus if focus were changed.
            predictionElement = elementWithFocus.PredictFocus(_focusMoveValue);

            var controlElement = predictionElement as Control;

            // If a ContentElement.
            if (controlElement != null)
            {
                controlElement.Foreground = Brushes.DarkBlue;
                controlElement.FontSize += 10;
                controlElement.FontWeight = FontWeights.ExtraBold;

                // Fields used to reset the UI when the mouse 
                // button is released.
                _focusPredicted = true;
                _predictedControl = controlElement;
            }
        }
    }
}

焦點移往下一個元素:

  1. 獲取FocusNavigationDirection和移焦請求TraversalRequest
  2. 獲取當前焦點元素。
  3. 執行MoveFocus(request)
private void OnMoveFocus(object sender, RoutedEventArgs e)
{
    // Creating a FocusNavigationDirection object and setting it to a
    // local field that contains the direction selected.
    var focusDirection = _focusMoveValue;

    // MoveFocus takes a TraveralReqest as its argument.
    var request = new TraversalRequest(focusDirection);

    // Gets the element with keyboard focus.
    var elementWithFocus = Keyboard.FocusedElement as UIElement;

    // Change keyboard focus.
    elementWithFocus?.MoveFocus(request);
}

恢復下一個焦點元素的屬性改變:

// Resets the UI after PredictFocus changes the UI.
private void OnPredictFocusMouseUp(object sender, RoutedEventArgs e)
{
    if (_focusPredicted)
    {
        _predictedControl.Foreground = Brushes.Black;
        _predictedControl.FontSize -= 10;
        _predictedControl.FontWeight = FontWeights.Normal;

        _focusPredicted = false;
    }
}
相關文章
相關標籤/搜索