我可以得到MouseEnter
,MouseLeave
和Click
事件,但不是MouseLeftButtonDown
或MouseLeftButtonUp
。Silverlight MouseLeftButtonDown event not firing
這裏是我的XAML
<UserControl x:Class="Dive.Map.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Canvas x:Name="LayoutRoot" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
<Button x:Name="btnTest" Content="asdf" Background="Transparent" MouseLeftButtonDown="btnTest_MouseLeftButtonDown"></Button>
</Canvas>
</UserControl>
這裏是我的代碼
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnTest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
btnTest.Content = DateTime.Now.ToString();
}
private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = false;
}
}
我在做什麼錯?
我有一個按鈕,作爲拖放手柄。我想要檢測用戶何時按下按鈕,然後我可以在MouseMove期間開始重新定位控件,然後停止在MouseUp事件上重新定位。我該怎麼做呢? – Matt
@Matt:停止使用拖放控制按鈕?這真的取決於__why__你想要這個。例如,如果您正在創建某種UI構建器,則可以將該按鈕放置在一個網格中,該網格也具有覆蓋該按鈕的透明畫布。然後,您可以管理畫布上的事件。注意畫布和按鈕將是兄弟,該按鈕不會是畫布的子項。 – AnthonyWJones
很酷。我最終爲我的按鈕創建了一個內容模板,然後在ControlTemplate元素上監聽MouseDown/MouseUp事件。 – Matt