简体   繁体   English

更改按钮的背景色

[英]Changing the Background color of a button

I have written this code: 我写了这段代码:

public partial class MainWindow : MetroWindow
{
    private static byte[] _buffer = new byte[1024];
    private static List<Socket> _clientSockets = new List<Socket>();
    private static Socket _serverSocket = new Socket
        (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        SetupServer();
    }

    public void changeColorButton()
    {
        var custom = new BrushConverter();
        testButton.Background = (Brush)custom.ConvertFrom("#FF00D400");
    }

    private static void SetupServer()
    {
        _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
        MessageBox.Show("Server ONLINE");
        _serverSocket.Listen(5);
        _serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
    }

    private static void AcceptCallBack(IAsyncResult AR)
    {
        Socket socket = _serverSocket.EndAccept(AR);
        _clientSockets.Add(socket);

        -----<triggers the changeColorButton() here>---

        s.Add(socket);
        socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
        _serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
    }

}

It accepts clients that will try to connect into the server. 它接受将尝试连接到服务器的客户端。 I also have a button for testing. 我也有一个测试按钮。 I am trying to change the colour of the button when a client tries to connect to the server. 当客户端尝试连接到服务器时,我正在尝试更改按钮的颜色。

I tried to call changeColorButton() using this code: 我试图使用以下代码调用changeColorButton():

    MainWindow m = new MainWindow();
    m.changeColorButton;

but it seems that I got an error on this one. 但似乎我在这方面犯了一个错误。 Any suggestions on how I can change the colour of the button on whenever there is a client that will try to connect to the server? 关于如何在有客户端尝试连接到服务器时如何更改按钮颜色的任何建议? thank you. 谢谢。 :) :)

尝试使用ConvertFromString而不是ConvertFrom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM