tags:

views:

1922

answers:

2

I'm new to WPF and am trying to make my first WPF desktop application using VC# Express.

I'm trying to get make three open file dialogs complete with text fields that show the specified path if the user chooses a file. I found working code to make the dialog box appear at the click of a button, but how do I get a text field to update/bind to the file path?

Something similar to how the file input boxes in HTML work would be ideal.

...

EDIT:

Okay I read the post just below mine and found the solution...

Now, how about redirecting console output to a text field?

A: 

If i understand you correctly Use the FileDialog.FileName to the the Full path .. and bind that to your text box. 76mel

76mel
+1  A: 

To answer your question about redirecting console output:

You'll be better off changing the code to fire an event with the string you wish to output. Then in the UI add a handler for that event and in the handler update the text field.

To declare an event add something like this code in your processing class:

public event EventHandler<StringEventArgs> Process_Message;

where StringEventArgs is a class based on EventArgs that wraps the message for sending.

To fire the event add something like this code in your processing class:

Process_Message(this, new StringEventArgs(message));

To attach a message handler in your UI class:

process.Process_Message += Process_Message;

To handle the event add something like this code to your UI class:

private void Process_Message(object sender, StringEventArgs e)
{
     Action action = () => UpdateStatus(e.Message);
     {
         Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, action);
     }
     else
     {
            action();
     }         
}

You need to do the threading test as the UI can't be updated from a different thread.

Then the UpdateStatus method:

private void UpdateStatus(string message)
{
    statusTextBox.Text = message;
}

Obviously you'll need to rename things to be appropriate to your application.

Look up events and EventArgs in the MSDN.

ChrisF