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

CaptureUnCaptureMouse捕獲鼠標

clipboard.png

關注點:
一、 獲取 捕獲鼠標按鈕的名字 的顯示ide

  1. 兩種方法:一是直接獲取GotMouseCapture路由事件中肯定事件引起者e.source的名字
  2. 二是新建一個IInputElement,獲取鼠標捕獲的元素Mouse.Captured;獲得按鈕的引用便可
// GotMouseCapture event handler
// MouseEventArgs.source is the element that has mouse capture
private void ButtonGotMouseCapture(object sender, MouseEventArgs e)
{
    var source = e.Source as Button;

    if (source != null)
    {
        // Update the Label that displays the sample results.
        lblHasMouseCapture.Content = source.Name;
    }
    // Another way to get the element with Mouse Capture
    // is to use the static Mouse.Captured property.
    else
    {
        // Mouse.Capture returns an IInputElement.
        IInputElement captureElement;

        captureElement = Mouse.Captured;

        // Update the Label that displays the element with mouse capture.
        lblHasMouseCapture.Content = captureElement.ToString();
    }
}

二、 點擊Capture Mouse按鈕時,使RaidoButton指示的鼠標 執行 被鼠標捕獲狀態。此時鼠標變大??就像鼠標放於按鈕上狀態。。。答:根據按鈕上鼠標捕獲定義,意味着捕獲鼠標就是默認鼠標在按鈕上拖移狀態。spa

private void OnCaptureMouseRequest(object sender, RoutedEventArgs e)
{
    Mouse.Capture(_elementToCapture);
}

private IInputElement _elementToCapture;

private void OnRadioButtonSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        switch (source.Content.ToString())
        {
            case "1":
                _elementToCapture = Button1;
                break;
            case "2":
                _elementToCapture = Button2;
                break;
            case "3":
                _elementToCapture = Button3;
                break;
            case "4":
                _elementToCapture = Button4;
                break;
        }
    }
}

CommandSourceControlUsingSystemTime使用系統時間作比較值的命令源控件

clipboard.png

一、實現效果線程

  1. 滑塊的滾動、點擊的增減值設置
  2. 同步顯示當前時間秒數
  3. 執行命令綁定方法,及判斷命令查詢狀態

二、關鍵詞code

  1. CommandBinding
  2. Slider.DecreaseSmall.Execute
  3. Timer.Elasped

三、靜態組織
window的命令綁定設置:orm

  1. Command綁定到window的一個靜態自定義路由命令
  2. 命令附加執行方法Executed,自定義方法附加到此句柄
  3. 命令可否執行CanExecute,附加判斷方法,返回bool
<!-- Command Binding for the Custom Command -->
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:MainWindow.CustomCommand}"
                Executed="CustomCommandExecuted"
                CanExecute="CustomCommandCanExecute" />
</Window.CommandBindings>

完整滑塊狀態設置:事件

<Border BorderBrush="DarkBlue"
    BorderThickness="1"
    Height="75"
    Width="425">
    <StackPanel Orientation="Horizontal">
        <Button Command="Slider.DecreaseSmall"
        CommandTarget="{Binding ElementName=secondSlider}"
        Height="25"
        Width="25"
        Content="-"/>
        <Label VerticalAlignment="Center">0</Label>
        <Slider Name="secondSlider"
        Minimum="0"
        Maximum="60"
        Value="15"
        TickFrequency="5"
        Height="50"
        Width="275" 
        TickPlacement="BottomRight"
        LargeChange="5"
        SmallChange="5" 
        AutoToolTipPlacement="BottomRight" 
        AutoToolTipPrecision="0"
        MouseWheel="OnSliderMouseWheel"
        MouseUp="OnSliderMouseUp" />
        <Label VerticalAlignment="Center">60</Label>
        <Button Command="Slider.IncreaseSmall"
        CommandTarget="{Binding ElementName=secondSlider}"
        Height="25"
        Width="25"
        Content="+"/>
    </StackPanel>
</Border>

四、動態流程
設置計時器:顯示隨時間秒數更新的時間數字ip

  1. 新建計時器類Timer,引起時間間隔事件Elapsed
  2. 設置時間間隔及激活計時器
  3. 線程調度執行同步委託
  4. Label顯示如今時間秒數,同時強制更新註冊命令狀態
// System Timer setup.
var timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Enabled = true;

// System.Timers.Timer.Elapsed handler
// Places the delegate onto the UI Thread's Dispatcher
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Place delegate on the Dispatcher.
    Dispatcher.Invoke(DispatcherPriority.Normal,
        new TimerDispatcherDelegate(TimerWorkItem));
}

// Method to place onto the UI thread's dispatcher.
// Updates the current second display and calls 
// InvalidateRequerySuggested on the CommandManager to force the
// Command to raise the CanExecuteChanged event.
private void TimerWorkItem()
{
    // Update current second display.
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raie the RequerySuggested event.
    CommandManager.InvalidateRequerySuggested();
}

判斷命令可否執行:ci

// CanExecute Event Handler.
//
// True if the current seconds are greater than the target value that is 
// set on the Slider, which is defined in the XMAL file.
private void CustomCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (secondSlider != null)
    {
        e.CanExecute = DateTime.Now.Second > secondSlider.Value;
    }
    else
    {
        e.CanExecute = false;
    }
}

滑塊中輪滾動事件,引發滑塊值增減固定值element

  1. 獲取滑塊引用
  2. 判斷滾動方向值,增減滑塊值Slider.DecreaseSmall.Execute
private void OnSliderMouseWheel(object sender, MouseWheelEventArgs e)
{
    var source = e.Source as Slider;
    if (source != null)
    {
        if (e.Delta > 0)
        {
            // Execute the Slider DecreaseSmall RoutedCommand.
            // The slider.value propety is passed as the command parameter.
            Slider.DecreaseSmall.Execute(
                source.Value, source);
        }
        else
        {
            // Execute the Slider IncreaseSmall RoutedCommand.
            // The slider.value propety is passed as the command parameter.
            Slider.IncreaseSmall.Execute(
                source.Value, source);
        }
    }
}

ps:此示例的e.Prameter爲空,不顯示秒數。路由

相關文章
相關標籤/搜索