简体   繁体   中英

Added event handler in Main Window for the Button, which is in the Page does not work

I'm using WPF for my very simple project. I've created login page using Page class and there are two controls there. They are Button and TextBox. Then I'm using this page in main window through the Frame. In main window I'm trying to add event handler to the Button on 'Click' event and somewhy it doesn't work. There are no error messages or somthing else. I'he tried to debug but it just does not enter the method.

Here is the Page code:

<Page x:Class="BJack.LoginPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="LoginPage">

    <Page.Resources>
        <Style TargetType="TextBox" x:Key="txtHint">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="False">
                    <Setter Property="Text" Value="Login" />
                    <Setter Property="Foreground" Value="#FFB8A8A8" />
                </Trigger>
            </Style.Triggers>
            <Style.Setters>
                <Setter Property="Height" Value="25" />
                <Setter Property="FontSize" Value="16" />
                <Setter Property="Width" Value="300" />
            </Style.Setters>
        </Style>
    </Page.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel VerticalAlignment="Center">
            <TextBox Name="txtLogin" Style="{StaticResource ResourceKey=txtHint}"></TextBox>
            <Button Grid.Row="1" Height="30" Width="150" Margin="0 30 0 0" Name="enterButton">Enter</Button>
        </StackPanel>

    </Grid>
</Page>

And here is a MainWindow code:

public partial class MainWindow : Window
{
    LoginPage lPage = new LoginPage();

    public MainWindow()
    {
        InitializeComponent();
        InitializeProgramm();
    }

    private void InitializeProgramm()
    {
        mainFrame.NavigationService.Navigate(new Uri("LoginPage.xaml", UriKind.Relative));
        lPage.enterButton.Click += enterButton_Click;
    }

    void enterButton_Click(object sender, RoutedEventArgs e)
    {
        if (lPage.txtLogin.Text.Length > 0)
        {
            mainFrame.Navigate(new Uri("GamePage.xaml", UriKind.Relative));
            this.WindowState = System.Windows.WindowState.Maximized;
        }
    }

    private void mainWin_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Keyboard.ClearFocus();
    }
}

here is a solution

private void InitializeProgramm()
{
    mainFrame.NavigationService.Navigate(lPage);
    lPage.enterButton.Click += enterButton_Click;
}

this will set the instance lPage as the content of the mainFrame on which you've attached the event handler instead of a newly created instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM