【问题标题】:Execute command on mouse click on image control wpf在图像控件 wpf 上单击鼠标执行命令
【发布时间】:2011-07-01 13:24:37
【问题描述】:

wpf 中有一个命令属性,我试图在单击图像时执行它。我基本上是在改变滚动条的风格。我从here 下载了示例样式,在更改了我的样式后,我得到了类似的结果:

好的,让我解释一下。在顶部,图像顶部有一个向上的箭头。我计划摆脱顶部箭头,但我需要它的原因是因为在 xaml 中它有一个命令,当单击它时它会向上滚动。向上箭头的代码是:

<RepeatButton
          Style="{StaticResource ScrollBarButton}"
          Margin="0,15,0,0"
          VerticalAlignment="Top"
          Background="#FFFFFFFF"
          Grid.Row="0"                                                                        
          Command="{x:Static ScrollBar.LineUpCommand}"   <!-- This is the line that enables to scroll upwards when clicked -->
          theme:ScrollChrome.ScrollGlyph="UpArrow"
          RenderTransformOrigin="0.5, 0.5">
          <RepeatButton.RenderTransform>
          <ScaleTransform ScaleX="4" ScaleY="2"/>
          </RepeatButton.RenderTransform>    
</RepeatButton>

简而言之,我对以下属性感兴趣:

Command="{x:Static ScrollBar.LineUpCommand}"

如果我可以摆脱顶部箭头并将该命令放置在图像中,那就太好了。问题是图像控件没有属性命令。我知道我可以使顶部箭头的 alpha 等于 0 并使它看起来好像只有一个图像,但我很好奇了解它是如何工作的,而且我想添加更多功能,例如更改图像外观鼠标输入等等。

【问题讨论】:

    标签: wpf xaml resources triggers styles


    【解决方案1】:

    创建一个带有控件模板的按钮,该模板只有一个图像并绑定到 按钮的命令。更多信息可以在这里找到: Attach ICommand in WPF UserControl

    【讨论】:

    • 我只是放置了一个按钮而不是图像。但是由于某种原因,当我将鼠标按下时它不起作用。
    • 尝试模板化一个重复按钮,按钮只引发一次事件。
    【解决方案2】:

    或者尝试使用 mvvm light takeit 中提供的 eventtocommand

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      相关资源
      最近更新 更多