C#命令语句使用

1、 主界面使用控件,绑定命令

<ItemsControl ItemsSource="{Binding ViewServerInfos}"
                              Style="{StaticResource ItemControlsStyle}"
                              Margin="8">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <local:UCItem ServerIP="{Binding ViewServerIP}"
                                              OnlineState="{Binding OnlineState}"
                                              DelCmd="{Binding Command}"/>
                                    </DataTemplate>

//ViewServerInfo类定义如下

public class ViewServerInfo
    {
        public string ViewServerIP { get; set; }

    public int ViewServerPort { get; set; }

    public bool OnlineState { get; set; }

    public string UserName { get; set; }

    public string UserPwd { get; set; }

    public RelayCommand<string> Command { get; set; }

    public event Action<string> DelEvent;

    public ViewServerInfo()
    {
        Command = new RelayCommand<string>(e =>
        {
            DelEvent?.Invoke(ViewServerIP);
        });
    }

}

2、 主程序中对事件的绑定

result.DelEvent += Result_DelEvent;		//添加ViewServerInfo时即进行绑定

3、 自定义控件命令绑定时,一定要在自定义控件生成时,声明有此项

public RelayCommand<string> DelCmd
        {
            get { return (RelayCommand<string>)GetValue(DelCmdProperty); }
            set { SetValue(DelCmdProperty, value); }
        }

    public static readonly DependencyProperty DelCmdProperty =
        DependencyProperty.Register("DelCmd", typeof(RelayCommand<string>), typeof(UCItem), new PropertyMetadata(null));

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DelCmd?.Execute(ServerIP);
    }

//点击按钮时触发此命令,然后触发绑定的命令,然后触发事件进行删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值