给自定义用户控件传递参数

假设放用户控件的页面为:WebForm1.aspx,用户控件为:WebUserControl1.ascx。

为用户控件传递参数只需要在用户控件WebUserControl1.ascx中添加属性,在WebForm1.aspx中为用户控件传入参数就可以了。例如:<uc1:webusercontrol1 id="WebUserControl11" runat="server" Para="传递参数"></uc1:webusercontrol1>。以下是代码:

 WebForm1.aspx

<% @ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx"  %>
<% @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
< HEAD >
< title > WebForm1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 WPF 中的 Button 类并实现 ICommandSource 接口来创建一个自定义控件,该控件可以带有参命令。首先,创建一个类,继承 Button 类并实现 ICommandSource 接口,如下所示: ```csharp public class CommandButton : Button, ICommandSource { public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( "CommandParameter", typeof(object), typeof(CommandButton), new PropertyMetadata(default(object))); public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof(ICommand), typeof(CommandButton), new PropertyMetadata(default(ICommand))); protected override void OnClick() { if (Command != null) { Command.Execute(CommandParameter); } base.OnClick(); } } ``` 在这个自定义控件中,我们定义了一个名为 CommandButton 的类,其中包含一个名为 CommandParameter 的依赖属性和一个名为 Command 的依赖属性。我们还重写了 OnClick 方法,在单击按钮时执行与命令相关的操作。 现在,您可以在 XAML 中使用这个自定义控件,并为 Command 和 CommandParameter 属性设置值,如下所示: ```xml <local:CommandButton Content="Click Me!" Command="{Binding MyCommand}" CommandParameter="parameter" /> ``` 其中,local 是指您的自定义控件所在的命名空间。MyCommand 是您的 ViewModel 中实现的命令,parameter 是该命令的参。当用户单击按钮时,命令将被执行,并且参将被传递给命令的 Execute 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值