WPF XAML使用鼠标操作事件

时间:2015-06-13 04:41:10

标签: c# wpf xaml

我正在学习WPF 4.5 Unleashed书。当我尝试通过手动键入代码将鼠标操作事件(如MouseEnter或MouseDoubleClick)合并为Button时,编译器告诉我它无法找到鼠标操作事件的参考。但是,当我使用双Tab键快捷方式时,一切正常。可能是什么问题?我在下面加粗了麻烦代码。

<Window x:Class="WpfApplicationLearning0001.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="513.265" Width="748.469">
<DockPanel>
    <Menu DockPanel.Dock="Top">

    </Menu>
    <StackPanel Name="ButtonBar" Orientation="Horizontal" DockPanel.Dock="Right">
        <StackPanel.LayoutTransform>
            <RotateTransform Angle="90">
            </RotateTransform>
        </StackPanel.LayoutTransform>
        **<Button Name="Panel1Button"  MouseEnter="Panel1Button_MouseEnter">
            Toolbox
        </Button>**


    </StackPanel>
    <Grid Background="White" Margin="0,0,2,3">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0"  Grid.Column="0" Grid.ColumnSpan="2" Background="Black" Foreground="White"  HorizontalContentAlignment="Center" Content= "Start Page"/>
    <GroupBox Grid.Row="1" Grid.Column="0" Background="White" Foreground="Black" Header="Start" />
    <GroupBox Grid.Row="2" Grid.Column="0" Background="White" Foreground="Black" Header="Recent" />
    <GroupBox Grid.Row="3" Grid.Column="0" Background="White" Foreground="Black" Header="Option" />
    <GroupBox Grid.Row="1" Grid.Column="1" Grid.RowSpan="3" Background="White" Foreground="Black" Header="Get Start"> 
         <ListBox>
            <ListBoxItem>Article number1</ListBoxItem>
            <ListBoxItem>Article number2</ListBoxItem>
            <ListBoxItem>Article number3</ListBoxItem>
            <ListBoxItem>Article number4</ListBoxItem>
        </ListBox>

    </GroupBox>


</Grid>
</DockPanel>

1 个答案:

答案 0 :(得分:2)

双击不仅将其放入XAML代码中,还将其创建在.cs文件中的代码:

private void Panel1Button_MouseEnter(object sender, MouseEventArgs e)
{

}

在您自己添加代码之前,如果您手动执行此操作,则错误是正确的。此外,当手动编码时,请务必在函数调用中包含(object sender, MouseEventArgs e),否则它可能无法将其识别为对MouseEvent的有效函数调用。