简体   繁体   中英

Set WPF Textbox Value in innerclass

It may be a bit of a noob question but I'm not an experienced programmer.

I am using WCF in combination with WPF to create a chatroom with a GUI. My problem is that I would like to use the callbackhandler to set the value of a textbox with incoming messages. Because this is an innerclass I cannot however call the textbox. Does anybody know a solution to this?

namespace WPFClient

public partial class MainWindow : Window
{
    Service1Client s;
    public MainWindow()
    {
        InitializeComponent();
        InstanceContext site = new InstanceContext(new CallbackHandler());
        s = new Service1Client(site);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Message m = new Message();
        m.Content = txtMessage.Text;
        m.User = txtName.Text;
        s.SendMessage(m);
    }

    public class CallbackHandler : IService1Callback
    {
        public void SendMessageToClients(Message m)
        {

            //I would like to call an alrdy generated textbox here to set its value, like txtMessageAll.Text("Setting text");
        }
    }
}

}

Thanks!

As CallbackHandler is a custom class, you can pass the TextBox from MainWindow to this class through parameterised constructor, while creating the object. As the reference is being passed you can change the Text of that TextBox through your Callback Handler class also.

 public class CallbackHandler 
    {
        public TextBox textValue { get; set; }

        CallbackHandler(TextBox tb) {

            this.textValue = tb;

        }
        public void SendMessageToClients(Message m)
        {
            this.textValue.Text="some_message";
            //I would like to call an alrdy generated textbox here to set its value, like txtMessageAll.Text("Setting text");
        }
    }

And from your MainWindow class

InstanceContext site = new InstanceContext(new CallbackHandler(txtboxMessageAll));

Where "txtboxMessageAll" is the TextBox which is already present in Xaml page.

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