我用一个按钮创建了一个新的UserContol。我想将按钮命令绑定到新用户控件的依赖属性。按钮命令绑定不起作用
<Grid>
<Button Name="Button1" Command="{Binding Button1Command}" />
</Grid>
这是DP在含用户控件:
public ICommand Button1Command
{
get { return (ICommand)GetValue(Button1CommandProperty); }
set { SetValue(Button1CommandProperty, value); }
}
public static readonly DependencyProperty Button1CommandProperty =
DependencyProperty.Register("Button1Command", typeof(ICommand), typeof(BptCellTemplate), new FrameworkPropertyMetadata(null));
当我尝试使用它没有发生,当我按下按钮。它不识别该命令。如果我添加一个事件它的作品。像这样:
public static readonly DependencyProperty Button1CommandProperty =
DependencyProperty.Register("Button1Command", typeof(ICommand), typeof(BptCellTemplate), new FrameworkPropertyMetadata(null, OnButton1CommandChanged));
private static void OnButton1CommandChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs args)
{
var bptCellTemplate = dependencyObject as BptCellTemplate;
if (bptCellTemplate == null || !(args.NewValue is ICommand))
{
return;
}
(bptCellTemplate.DataContext as BptCellTemplateViewModel).Button1Command = (ICommand)args.NewValue;
}
有没有一种方法来绑定它没有事件?因为它与我做了同样的方式与其他按钮的属性(Visibility
例如)的作品
您在哪里设置了Button1Command的值? – jure 2013-05-07 08:19:49