Using CheckBoxes in Silverlight

烈酒焚心 提交于 2020-01-06 07:57:06

问题


I am using a checkbox in a child window in Silverlight.

When a user clicks the checkbox this sends sends a toggle function to a database, where if the checkobox is checked, the value = 1, and unchecked value = -1, so the toggle is changing these values.

By using the following code-

private int checkCounter1 = 0;

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
    if (checkCounter1 == 0)
    {
    }
    else
    {
        //Web Service toggle function 
    }

    checkCounter1 = 1;
}

private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
    if (checkCounter1 == 0)
    {
    }
    else
    {
        //Web Service toggle function
    }

    checkCounter1 = 1;
}

When the user clicks the checkbox this is invoking the webservice call as expected and the value is getting toggled.

My problem being is that when the child window is loaded up, this for some reason is invoking the web service call on the checkBox1_Checked. I do not know why. So by adding the checkCounter I was hoping this would skip past the toggle function on page load.

On page load I am using another web service call to call the values of the checkbox at present, for intsance if in the database the value is = to 1, then the checkbox should be checked on the form -

void Settings_Loaded(object sender, RoutedEventArgs e)
{
    cosainWebService.Service1SoapClient();
    client.callFlagsCompleted += new EventHandler<callFlagsCompletedEventArgs>(client_callFlagsCompleted);
    client.callFlagsAsync();
}

So I then take the result of callFlags and use it here-

void client_callFlagsCompleted(object sender, callFlagsCompletedEventArgs e)
{            
    string flag = System.Convert.ToString(e.Result);

    foreach (string x in e.Result)
    {
        string[] array = x.Split(',');

        if (array[1] == "Checkbox Name" ) 
        {
            if (array[0] == "1") //this is the value in the database
            {
                checkBox1.IsChecked = true;
            }
            else
            {

            }
        }
    }
}

The checkbox XAML looks like-

<CheckBox Content="CheckBox Name" Height="16" HorizontalAlignment="Left" Margin="12,42,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked"/>

So when debugging, clicking the checkbox is toggling the value in the database, so when a user has checked the box, the value should be stored as '1', I should then be able to close the window, load it up again, and the box should be ticked, but for some reason this is not working, and when the window opens, it is toggling the value. Where am I going wrong?


回答1:


I think this is the sequence of events causing your webservice to be fired at page load.

  1. In design mode this checkbox1 is checked.
  2. In run mode when this window gets loaded, it calls InitializeComponents() (method which initializes controls on window. Don't know real name of this method in SL but in Winforms and WPF its InitializeComponents).

  3. There it checks the checkbox because that's the way its in design mode. This causes the event to be fired which call your service.

First don't save value in database as 1 or -1.

Save as 0 or 1. These are int values and directly convertible to bool's true and false which can further be assigned to your checkbox.

In UI don't check your checkbox. In Window Load when data is retrieved from database, do this

void Windows_Loaded(....)
{
    bool i = value_from_database(); //------- Line No. 1
    checkBox1.Checked -= new RoutedEventHandler(checkBox1_Checked); //Line No. 2
    checkbox1.Ischecked = i; //------- Line No. 3
    checkBox1.Checked += new RoutedEventHandler(checkBox1_Checked); //Line No. 4
}

When you assign database value to checkbox, its checked event will not fire (since it has been removed by Line No. 2).

Now when it assigns that value to it. (Line 3)

We will again add that event to our checkbox. (Line 4)



来源:https://stackoverflow.com/questions/10616020/using-checkboxes-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!