在Silverlight中使用复选框 [英] Using CheckBoxes in Silverlight

查看:111
本文介绍了在Silverlight中使用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Silverlight的子窗口中使用复选框。

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

当用户单击复选框时,此发送将向数据库发送切换功能,如果复选框选中,则该值= 1,而未选中的值= -1,因此该切换更改了这些值。

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.

通过使用以下代码-

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;
}

当用户单击复选框时,这将按预期方式调用Web服务调用,并且

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

我的问题是,在加载子窗口时,出于某种原因,这会在 checkBox1_Checked 。我不知道为什么。因此,通过添加 checkCounter ,我希望它会跳过页面加载时的切换功能。

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.

页面加载时我目前正在使用另一个Web服务调用来调用该复选框的值,例如,如果在数据库中该值= = 1,则应在以下格式上选中该复选框-

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();
}

所以我将callFlags的结果用于此处-

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
            {

            }
        }
    }
}

复选框XAML看起来像-

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"/>

因此,在调试时,单击复选框会切换数据库中的值,因此当用户检查时框,该值应存储为 1,然后我应该能够关闭该窗口,再次将其加载,并勾选该框,但由于某些原因,该框不起作用,并且当窗口打开时,它在切换价值。我要去哪里了?

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?

推荐答案

我认为这是导致您的Web服务在页面加载时被触发的事件序列。 / p>

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


  1. 在设计模式下,选中此复选框1。

  2. 在运行模式下,当此窗口加载后,它将调用InitializeComponents()
    (在窗口上初始化控件的方法。不知道此方法在SL中的真实名称,但在Winforms和WPF中是它的InitializeComponents)。

  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).

在此选中该复选框,因为这是设计模式下的方式。

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.

首先不要在数据库中将值保存为 1 -1

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

另存为 0 1 。这些是 int 值,可以直接转换为bool的 true false 可以进一步分配给您的复选框。

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
}

将数据库值分配给复选框时,选中事件不会触发(因为它已被第2行删除)。

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

现在,当它为其分配值时。 (第3行)

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

我们将再次将该事件添加到我们的复选框中。 (第4行)

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

这篇关于在Silverlight中使用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆