![]() The Ok button has an event handler which ensures that the special DialogResult property of the Window is set to true when clicked, to signal to This allows us to place the question and theĭefault answer, if provided, into the designated UI controls. In Code-behind, I changed the constructor to take two parameters, with one being optional. An event handler is needed to set the DialogResult for this though, as described later. The IsDefault property gives focus to the Ok button and also ensures that if the user presses the Enter key on their keyboard, this button This also ensures that the user can press the Esc key on their keyboard to close the window, something that should always be possible in a WPF that if the user clicks this button, the DialogResult of the Window should be set to false which will also close the window. Position decided by Windows, unless you manually specify Top and Left properties for it.Īlso pay special attention to the two properties I've used on the dialog buttons: IsCancel and IsDefault. Other non-main windows, you should change this value to CenterScreen or CenterOwner, to change the default behavior where your window will appear in a For a dialog like this, and probably for most Instead, use margins and minimum sizes to ensure that things look the way you want them to, while still allowing the user toĪnother property which I've changed on the Window is the WindowStartupLocation property. Window and instead set it to automatically resize to match the content - this makes sense in a dialog, so you don't have to fine tune the size to makeĮverything look alright. I've removed the Width and Height properties of the In the XAML part, I've used a Grid for layout of the controls - nothing fancy here. The code is pretty simple, but here are the things that you should pay special attention to: XAML Private void Window_ContentRendered(object sender, EventArgs e) Private void btnDialogOk_Click(object sender, RoutedEventArgs e) Public InputDialogSample(string question, string defaultAnswer = "") Public partial class InputDialogSample : Window I decided to add an icon to the dialog as well, for good looks. This article, we'll create a very simple dialog to ask the user a question and then return the answer, while discussing the various good practices that youįor this particular dialog, I just wanted a Label telling the user which information we need from him/her, a TextBox for entering the answer, and then the However, there are a few things that you should remember when creating dialogs, to ensure that your application acts like other Windows applications. Need to create a Window, place the required controls in it and then show it. ![]() In the last couple of articles, we've looked at using the built-in dialogs of WPF, but creating your own is almost just as easy.
0 Comments
Leave a Reply. |