Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:25:08+00:00 2026-05-25T20:25:08+00:00

I have a WPF window which opens as a modal dialog. On dialog, I

  • 0

I have a WPF window which opens as a modal dialog.

On dialog, I have OK & Cancel buttons with IsDefault & IsCancel properties set to True for them respectively. Both the buttons have Click event handlers which close the dialog box.

Here is the relevant XAML:

<StackPanel Orientation="Horizontal" Grid.Row="1"  Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">
    <Button Content="OK"
                Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"                       
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
    <Button Content="Cancel"
                Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True"
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
</StackPanel>

Here is the code behind:

private void btnOK_Click(object sender, RoutedEventArgs e)
{
    // My some business logic is here                
    this.Close();
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

When I press Esc button on the keyboard (even when focus is not on the Cancel button), the dialog box gets closed as expected. However, when I press Enter key when focus is NOT on the OK button, nothing happens.

I have a DataGrid on the dialog. I want to close the dialog when I select any row in the data grid and press enter.

How to make this thing happen?

Some additional information: I have a text box on the dialog. And it has the event handler for the Keyboard.PreviewKeyDown event. When I am in the text box and I press enter, the dialog box should not be closed. But I can remove this handler. Important thing is to get above question resolved.

private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        this.Search(); // Does some searching
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:25:09+00:00Added an answer on May 25, 2026 at 8:25 pm

    Your code is working fine for me. it close dialog when I press enter. You can write e.Handled = true; line after your search functionality in tbxSearchString_PreviewKeyDown event. So it will not close dialog.

    <Grid>
            <TextBox Name="tbxSearchString" HorizontalAlignment="Left" Width="100" Height="30" Grid.Row="0" PreviewKeyDown="tt_PreviewKeyDown"></TextBox>
            <StackPanel Orientation="Horizontal" Grid.Row="1"  Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">
    
                <Button Content="OK" 
                    Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"                        
                    VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
                <Button Content="Cancel" 
                    Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True" 
                    VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
            </StackPanel>
        </Grid>
    

    Code behind

    private void btnOK_Click(object sender, RoutedEventArgs e)
            {
                DialogResult = true; 
            }
    
            private void btnCancel_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
            private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
            {
               if (e.Key == Key.Enter)
               {
                   this.Search();
                   e.Handled = true;
               }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF window, which contains a button called Cancel. Under ordinary circumstances
I have a WPF Window which has a among other controls hosts a Frame.
I have a WPF window for editing database information, which is represented using an
I have a window in WPF which shows some media contents. This content contains
I've got a WPF window where I have a column of buttons on the
I have a WPF Window that open a modal child window to load some
I am using WPF and C# I have a button that opens a window,
I have a WPF application which launches a WPF window. In the window, I
In my WPF application, I have a table which stores the frequently used window
In a WPF application, I have buttons which pop up instances of windows. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.