I need to create a Dialog / Prompt including TextBox for user input. My problem is, how to get the text after having confirmed the dialog? Usually I would make a class for this which would save the text in a property. However I want do design the Dialog using XAML. So I would somehow have to extent the XAML Code to save the content of the TextBox in a property - but I guess that's not possible with pure XAML. What would be the best way to realize what I'd like to do? How to build a dialog which can be defined from XAML but can still somehow return the input? Thanks for any hint!
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
You don't need ANY of these other fancy answers. Below is a simplistic example that doesn't have all the
Margin
,Height
,Width
properties set in the XAML, but should be enough to show how to get this done at a basic level.XAML
Build a
Window
page like you would normally and add your fields to it, say aLabel
andTextBox
control inside aStackPanel
:Then create a standard
Button
for Submission ("OK" or "Submit") and a "Cancel" button if you like:Code-Behind
You'll add the
Click
event handler functions in the code-behind, but when you go there, first, declare a public variable where you will store your textbox value:Then, for the event handler functions (right-click the
Click
function on the button XAML, select "Go To Definition", it will create it for you), you need a check to see if your box is empty. You store it in your variable if it is not, and close your window:Calling It From Another Page
You're thinking, if I close my window with that
this.Close()
up there, my value is gone, right? NO!! I found this out from another site: http://www.dreamincode.net/forums/topic/359208-wpf-how-to-make-simple-popup-window-for-input/They had a similar example to this (I cleaned it up a bit) of how to open your
Window
from another and retrieve the values:Cancel Button
You're thinking, well what about that Cancel button, though? So we just add another public variable back in our pop-up window code-behind:
And let's include our
btnCancel_Click
event handler, and make one change tobtnSubmit_Click
:And then we just read that variable in our
MainWindow
btnOpenPopup_Click
event:Long response, but I wanted to show how easy this is using
public static
variables. NoDialogResult
, no returning values, nothing. Just open the window, store your values with the button events in the pop-up window, then retrieve them afterwards in the main window function.The "responsible" answer would be for me to suggest building a ViewModel for the dialog and use two-way databinding on the TextBox so that the ViewModel had some "ResponseText" property or what not. This is easy enough to do but probably overkill.
The pragmatic answer would be to just give your text box an x:Name so that it becomes a member and expose the text as a property in your code behind class like so:
Then in your code behind...
Then to use it...
Great answer of Josh, all credit to him, I slightly modified it to this however:
MyDialog Xaml
MyDialog Code Behind
And call it somewhere else
I just add a static method to call it like a MessageBox:
And the code behind:
So you can call it like: