zoukankan      html  css  js  c++  java
  • CommandTarget属性

    CommandTarget属性:

    当一个command被executed的时候,指定这个事件的sender是谁。

    例如如下xaml:

    <StackPanel Name="FirstStackPanel"

    Background="AliceBlue"

    Focusable="True">

    <!-- <SnippetCustom_RoutedCommandCommandBinding> -->

    <StackPanel.CommandBindings>

    <CommandBinding Command="{x:Static custom:Window1.ColorCmd}"

    Executed="ColorCmdExecuted"

    CanExecute="ColorCmdCanExecute"/>

    </StackPanel.CommandBindings>

    <!-- <SnippetCustom_RoutedCommandCommandBinding> -->

     

    <Label>First StackPanel</Label>

     

    <!-- <SnippetCustom_RoutedCommandCommandSource> -->

    <Button Name="myButton" Command="{x:Static custom:Window1.ColorCmd}"

    CommandParameter="ButtonOne"

    CommandTarget="{Binding ElementName=FirstStackPanel}"

    Content="CommandTarget = FristStackPanel" />

    <!-- </SnippetCustom_RoutedCommandCommandSource> -->

    </StackPanel>

    对应后台的ColorCmdExecuted方法如下:

    private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)

    {

    Panel target = e.Source as Panel;

    if (target != null)

    {

    if (target.Background == Brushes.AliceBlue)

    {

    target.Background = Brushes.LemonChiffon;

    }

    else

    {

    target.Background = Brushes.AliceBlue;

    }

     

    }

    }

    则这里sender指的是CommandTarget所指的值,即FirstStackPanel,如果在xaml中不指定CommandTarget的话,这里的sender的值就会是触发这个commnadmyButton

  • 相关阅读:
    十四行诗 Sonnet 15
    P3386 【模板】二分图匹配
    20171105模拟题
    需要注意的各种各种 持续更新
    P1315 观光公交 贪心
    【搬家辣】
    【洛谷P2387】魔法森林
    【洛谷P3369】普通平衡树(splay)
    【算法详解】splay的初步了解
    研究性学习代码
  • 原文地址:https://www.cnblogs.com/bear831204/p/1527144.html
Copyright © 2011-2022 走看看