[英]How to binding a Command in Style in .xaml from .cs?
现在,我正在模拟MessageBox。 我在.xaml的Style.Template中构建了“ Close Button
,但是我不知道如何将命令与CloseCommand绑定。 是否可以使用“ 系统关闭命令”绑定?
.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(是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>
...
看到红色按钮 ,我不知道如何为它绑定命令:
这是整个解决方案:
.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:
<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
为按钮创建一个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 }
}
在xaml中
<Button x:Name="CloseButton" Command="{Binding Close}"/>
确保该按钮可使用DataContext
从类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));
使用TemplateBinding
这样绑定到Command:
<Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}"/>
由于命令是公开的,因此您可以像这样从外部绑定它:
<local:MessageBoxModule CloseCommand="{Binding ViewModelCommand}"/>
假设您已经拥有View模型,并且它已经包含要绑定关闭按钮的ICommand。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.