Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从.cs以.xaml的样式绑定命令?_C#_Wpf_Button_Command - Fatal编程技术网

C# 如何从.cs以.xaml的样式绑定命令?

C# 如何从.cs以.xaml的样式绑定命令?,c#,wpf,button,command,C#,Wpf,Button,Command,现在,我正在模拟MessageBox。我在.xaml中以Style.Template构建了Close按钮,但我不知道如何将命令与CloseCommand绑定。是否可以使用系统关闭命令进行binding操作 .cs(定义自定义控件): .xaml(是一个ResourceDictionary文件,为MessageBoxModule提供样式): .xaml: <ResourceDictionary > <Style TargetType="{x:Type local:Mess

现在,我正在模拟MessageBox。我在.xaml中以Style.Template构建了
Close按钮,但我不知道如何将命令与CloseCommand绑定。是否可以使用系统关闭命令进行binding操作

.cs(定义自定义控件):

.xaml(是一个ResourceDictionary文件,为MessageBoxModule提供样式):

.xaml:

<ResourceDictionary >
    <Style TargetType="{x:Type local:MessageBoxModule}">
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MessageBoxModule}">
                    <Border ...>
                        <Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}"/>
...

...
@潜艇

为按钮创建DataContext,如下所示

public class ButtonViewModel{

private RelayCommand _CloseCommand;

public ICommand CloseCommand
        {
            get { return _CloseCommand?? (_CloseCommand= new RelayCommand(p => Close())); }
        }

void Close(){

//Here u cn write the  logic which close the window from this view model or  raise an event which handled by your button container

}
}


    public class RelayCommand : ICommand
    {
        #region Fields

        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region Constructors

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion // Constructors

        #region ICommand Members [DebuggerStepThrough]

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion // ICommand Members }
    }
公共类按钮查看模型{
专用中继命令_CloseCommand;
公共ICommand CloseCommand
{
获取{return\u CloseCommand??(\u CloseCommand=newrelaycommand(p=>Close());}
}
无效关闭(){
//在这里,您可以编写从该视图模型关闭窗口或引发由按钮容器处理的事件的逻辑
}
}
公共类中继命令:ICommand
{
#区域字段
私有只读操作\u执行;
私有只读谓词_canExecute;
#endregion//字段
#区域构造函数
公共中继命令(操作执行)
:此(执行,空)
{
}
公共RelayCommand(操作执行,谓词canExecute)
{
如果(execute==null)抛出新的ArgumentNullException(“execute”);
_执行=执行;
_canExecute=canExecute;
}
#endregion//构造函数
#区域ICommand成员[DebuggerStepThrough]
公共布尔CanExecute(对象参数)
{
返回_canExecute==null | | | u canExecute(参数);
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
#endregion//ICommand成员}
}
在xaml中

<Button x:Name="CloseButton" Command="{Binding Close}"/>


确保DataContext可用于class
MessageBoxModule
中类型为
ICommand
的按钮

Expose
依赖项属性

public ICommand CloseCommand
{
   get { return (ICommand)GetValue(CloseCommandProperty); }
   set { SetValue(CloseCommandProperty, value); }
}

public static readonly DependencyProperty CloseCommandProperty =
   DependencyProperty.Register("CloseCommand", typeof(ICommand), 
                                typeof(MessageBoxModule));
使用
TemplateBinding
像这样绑定到命令:

<Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}"/>
<local:MessageBoxModule CloseCommand="{Binding ViewModelCommand}"/>

由于命令是公开的,所以您可以像这样从外部绑定到它:

<Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}"/>
<local:MessageBoxModule CloseCommand="{Binding ViewModelCommand}"/>


假设您已经准备好视图模型,并且它已经包含要绑定关闭按钮的ICommand。

请发布相关代码。如果您使用MVVM模式,您可以简单地实现这一点。@Binson Eldho是的,我使用MVVM模式,我该怎么做?很好。没问题……)嗯..我认为MessageBoxModule是一个非常简单的窗口,具有简单的功能,所以我不提供它的视图模型。我已经发布了整个解决方案,请参考:是的,如果您想通过快捷方式
Alt+F4
关闭它,可以使用键命令绑定:
<local:MessageBoxModule CloseCommand="{Binding ViewModelCommand}"/>