Nesse artigo vamos fazer um simples cadastro usando SilverLight.

1 - Vamos criar um projeto utilizando SilverLight, De o nome a ele de "MeuProjetoSilverLight"

2 - Insira na tela os seguintes componentes :

 ___________________________________________________
|Componente      | Nome    | Text                      |
 ---------------------------------------------------
|TextBox1         | txtCodigo   |                   |
|TextBox2         | txtNome    |                   |
|TextBox3         | txtTelefone |                   |
|TextBox4         | txtBairro   |                   |
|Label1           | lbCodigo    | Codigo :          |
|Label2           | lbNome    | Nome :            |
|Label3           | lbTelefone    | Telefone :        |
|Label4           | lbBairro    | Bairro :          |
|Button1          | btInserir   | Inserir           |
|Button2          | btGravar    | Gravar            |
|Button3          | btLimpar    | Limpar Registro   |
 ---------------------------------------------------
 
Obs: Para alterar o Texto do componente, se altera na propriedade "Content" ou altere direto no xaml, conforme a figura abaixo:




3 - O Visual do aplicativo devera ficar da seguinte forma :




4 - Antes do construtor do aplicativo insira o seguinte código :


    class Cliente
    {
       public Cliente(int codigo, string nome, string telefone, string bairro)
       {
           this.codigo = codigo;
           this.nome = nome;
           this.telefone = telefone;
           this.bairro = bairro;
        }

        public int codigo { get; set; }
        public string nome { get; set; }
        public string telefone { get; set; }
        public string bairro { get; set; }
    }

    List lstClientes = new List();



5 - Após o construtor do aplicativo insira o seguinte codigo :


    private void LimparCampos()
    {
        txtCodigo.Text = string.Empty;
        txtNome.Text = string.Empty;
        txtTelefone.Text = string.Empty;
        txtBairro.Text = string.Empty;
    }



6 - Agora de um duplo click no botão btInserir e adicione o seguinte código:


    LimparCampos();
    txtCodigo.Text = (lstClientes.Count + 1).ToString();
    txtNome.Focus();



7 - Agora de um duplo click no botão btLimpar e insira o seguinte código, que servira para limpar os registros da lista.


   lstClientes.Clear();



8 - Agora após o método "LimparCampos()" insira o seguinte código :



    private bool carrega_cliente(int pCodigo)
    {
        for (int i = 0; i < lstClientes.Count; i++)
        {
            if (lstClientes[i].codigo == pCodigo)
            {
                txtCodigo.Text = lstClientes[i].codigo.ToString();
                txtNome.Text = lstClientes[i].nome;
                txtTelefone.Text = lstClientes[i].telefone;
                txtBairro.Text = lstClientes[i].bairro;
                return true;
             }
         }
         return false;
     }




9 - Agora de um duplo click no botão gravar e insira o seguinte código :


    if (string.IsNullOrEmpty(txtCodigo.Text))
    {
       MessageBox.Show("Campo codigo nao pode estar em branco");
       return;
    }
    if (!carrega_cliente(Convert.ToInt16(txtCodigo.Text)))
    {
      lstClientes.Add(new Cliente(Convert.ToInt16(txtCodigo.Text), txtNome.Text, txtTelefone.Text, txtBairro.Text));
    }
    else
    {
       MessageBox.Show("Cliente já cadastrado, Dados não serao alterados");
    }   



10 - Agora no evento KeyDown do txtCodigo insira o seguinte código :


    if (e.Key == Key.Enter)
    {
       if (string.IsNullOrEmpty(txtCodigo.Text))
       {
           MessageBox.Show("Campo codigo nao pode estar em branco");
           return;
       }

       carrega_cliente(Convert.ToInt16(txtCodigo.Text));
    }



11 - Feito isso o nosso aplicativo está pronto, espero ter ajudado a ter uma idéia simples de como trabalhar com SilverLight.

Exp :



Agradeço a atenção, Obrigado e abraço a todos.