我希望在我的WPF應用程序中使用<Image>
響應鼠標點擊。開箱即可使用'Mouse Up'和'Moue Down'事件。我覺得這很特別。有沒有辦法擴展控件或使用其他控件來產生相同的效果?WPF圖像控制與點擊事件
35
A
回答
67
爲了擴展Shawn Mclean的回答,WPF的一個很棒的功能是能夠在完全改變控件的外觀的同時充分利用控件的行爲。如果你想創建一個圖像,其行爲就像一個按鈕(完成點擊事件,命令綁定,默認按鈕分配等),你可以在一個按鈕控件中放置一個圖像,然後重新啓動該按鈕以刪除按鈕「chrome」。這將給你一個按鈕的漂亮的API與你想要的外觀。您可以重複使用這種風格,如果您有命令綁定到新圖像按鈕,則此方法將減少在您的代碼中創建事件處理程序的需要。
創建這樣的風格很簡單。只需在資源中使用命名鍵創建一個新的樣式資源,並將此資源分配給按鈕的Style屬性。這裏是我扔在一起的例子:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ButtonStyleTestApp.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
<Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="#FF44494D">
<Button Style="{DynamicResource NoChromeButton}" Click="ButtonClicked" >
<Image Source="Desert.png" Stretch="None"/>
</Button>
</Grid>
</Window>
29
1
您可以使用一組4個處理器和2個布爾變量:
布爾:
- IsClicked
- IsEntered
處理程序:
- OnMouseDown-設置IsClicked爲true;
- OnMouseEnter- sets IsEntered true;
- OnMouseLeave - 設置IsEntered爲false;
- OnMouseUp - 如果(IsClicked & & IsEntered){/ TODO您的邏輯 /},然後套IsClicked假;
該構造實現了經典點擊的功能。
-1
- OnMouseLeave - 也設置isClicked爲false; 否則,您可以點擊鼠標並釋放鼠標。然後點擊鼠標懸停並釋放鼠標仍然會發生點擊。
相關問題
- 1. WPF:圖片點擊事件
- 2. 按鈕控制事件中點擊圖像不能觸發
- 3. 點擊事件上圖像控制Windows Phone 8.1
- 4. 圖像控件鍵盤事件WPF,UWP
- 5. 在鼠標點擊圖像控制時執行命令wpf
- 6. 在WPF內部的WPF控件上實現點擊事件
- 7. WPF Telerik gridview點擊事件
- 8. wpf datagrid點擊事件
- 9. 控件點擊事件點擊框
- 10. WPF用戶控件點擊
- 11. 點擊事件到圖像的一部分WPF
- 12. 點擊事件jQuery的控制命令
- 13. Javascript MVC控制器和點擊事件
- 14. AppWidget與背景圖像+文字+點擊(點按)事件
- 15. 快速WPF圖像控制
- 16. WPF圖像控制削波
- 17. WPF圖像控制源
- 18. 畫布圖像和點擊事件
- 19. Java中的圖像點擊事件LibGdx
- 20. 添加點擊事件圖像
- 21. Xamarin.Forms和Prism圖像點擊事件
- 22. 點擊移動圖像事件
- 23. 用點擊事件移動圖像
- 24. 如何鏈接swf文件與圖像點擊事件
- 25. 非javascript多行控制與點擊事件
- 26. 試圖複製點擊事件
- 27. 使WPF用戶控件標記點擊事件處理
- 28. 如何根據外部點擊事件控制高圖表
- 29. html圖像點擊事件以條件方式加載圖像
- 30. 打開點擊圖像內部圖像控制(asp.net/c#)
把圖像放在一個按鈕,按照http://stackoverflow.com/questions/2720463/creating-a-clickable-image-in-wpf – LosManos 2012-12-27 22:19:25
試試這個。 – VivekDev 2016-05-21 11:29:37