[英]How do I add a 'checked'/'unchecked' event handler to a checkbox in codebehind using Silverlight?
Okay this one's pretty straightforward. 好的,这很简单。 I'm creating a checkbox in my Page.xaml.cs file, and assigning it some params.
我正在Page.xaml.cs文件中创建一个复选框,并为其分配一些参数。 I need to link to events for checked and unchecked.
我需要链接到已选中和未选中的事件。 What is the syntax for this?
这是什么语法?
Also, my current code looks like this: 另外,我当前的代码如下所示:
CheckBox cb = new CheckBox();
cb.IsChecked = true;
System.Windows.Thickness t1 = new Thickness(425,10,0,0);
cb.Margin = t1;
cb.Content = "Checkbox1";
I tried adding my function to cb.Checked but it gives me the following error: "The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -=" 我尝试将函数添加到cb.Checked,但它给了我以下错误:“事件'System.Windows.Controls.Primitives.ToggleButton.Checked'只能出现在+ =或-=“的左侧
CheckHandler is defined separately CheckHandler单独定义
public void CheckHandler(Object obj, EventArgs e)
{
// Random stuff here
}
You don't show the syntax you were using that resulted in the error. 您不会显示所使用的语法导致错误。 Am I correct in assuming it was this:
我是否假设这是正确的:
cb.Checked = CheckHandler;
? ?
Because you wire up an event handler in C# like this: 因为您可以像这样在C#中连接事件处理程序,所以:
cb.Checked += CheckHandler;
cb.Checked += CheckHandler;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.