简体   繁体   English

如何从.cs绑定.xaml中的样式命令?

[英]How to binding a Command in Style in .xaml from .cs?

Now, I'm mocking up the MessageBox. 现在,我正在模拟MessageBox。 I build the Close Button in Style.Template in .xaml, but I don't know how to binding the command with CloseCommand. 我在.xaml的Style.Template中构建了“ Close Button ,但是我不知道如何将命令与CloseCommand绑定。 Whether it can bingding with System Close Command ? 是否可以使用“ 系统关闭命令”绑定?

.cs (define a custom control): .cs(定义自定义控件):

internal sealed class MessageBoxModule : Window
{
    #region Constructor
    static MessageBoxModule()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(MessageBoxModule),
            new FrameworkPropertyMetadata(typeof(MessageBoxModule)));
    }

    public MessageBoxModule()
    {
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        AllowsTransparency = true;
        WindowStyle = System.Windows.WindowStyle.None;
        ShowInTaskbar = false;

        try
        {
            Resources.Source = new Uri(@"/Wpf.Controls;component/Themes/Generic.xaml", UriKind.Relative);
        }
        catch
        { }
...

.xaml (is a ResourceDictionary file, provide a style for MessageBoxModule): .xaml(是ResourceDictionary文件,为MessageBoxModule提供样式):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Wpf.Controls">
        <Style TargetType="{x:Type local:MessageBoxModule}">
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:MessageBoxModule}">
                        <Border ...>
                            <Button x:Name="CloseButton".../>
                            ...
                        </Border>
...

see the red button , I don't know how to binding a command for it: 看到红色按钮 ,我不知道如何为它绑定命令:

在此处输入图片说明

This is The Entire solution: 这是整个解决方案:

.cs: .cs:

internal sealed class MessageBoxModule : Window
{
    public MessageBoxModule()
    {
        InputGestureCollection inputGestures = new InputGestureCollection();
        inputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt));
        CloseCommand = new RoutedCommand(
            "CloseCommand",
            typeof(MessageBoxModule),
            inputGestures);
        CommandBindings.Add(new CommandBinding(CloseCommand, CloseCommandExecuted));
    }

    public static readonly DependencyProperty CloseCommandProperty =
        DependencyProperty.Register(
            "CloseCommand", 
            typeof(RoutedCommand), 
            typeof(MessageBoxModule));

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

    public void CloseCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        Close();
    }
}

.xaml: .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}"/>
...

@SubmarineX @SubmarineX

Create a DataContext for the button Something like 为按钮创建一个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 }
    }

in xaml 在xaml中

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

Ensure that the DataContext is availble for the button 确保该按钮可使用DataContext

Expose dependency property of type ICommand from class MessageBoxModule : 从类MessageBoxModule公开ICommand类型的dependency property

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

public static readonly DependencyProperty CloseCommandProperty =
   DependencyProperty.Register("CloseCommand", typeof(ICommand), 
                                typeof(MessageBoxModule));

Use TemplateBinding to bind to Command like this: 使用TemplateBinding这样绑定到Command:

<Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}"/>

Since command is exposed, you can bind to it from outside like this: 由于命令是公开的,因此您可以像这样从外部绑定它:

<local:MessageBoxModule CloseCommand="{Binding ViewModelCommand}"/>

Assuming you already have View model in place and it already contains ICommand to which you want to bind close button. 假设您已经拥有View模型,并且它已经包含要绑定关闭按钮的ICommand。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM