问题描述
在主窗口的工作的CommandBinding罚款。
在它的UserControl1不工作。注意:在DataContext正确设置由按钮,这是一项具有约束力的结果的内容证明。
我不是要命令在该用户绑定在主窗口的命令或任何其他此类挂羊头卖狗肉。我只是试图复制我在主窗口中的UserControl1一样。
主窗口XAML
<&StackPanel的GT;
<按钮内容=点击这里命令={结合ClickHereCommand}HEIGHT =25WIDTH =90>< /按钮>
<局部:&的UserControl1 GT;< /地方:&的UserControl1 GT;
< / StackPanel的>
主窗口代码隐藏
公共部分类主窗口:窗口
{
公共静态的RoutedCommand ClickHereCommand {搞定;组; }
公共主窗口()
{
的InitializeComponent();
this.DataContext =这一点;
ClickHereCommand =新的RoutedCommand();
CommandBindings.Add(新的CommandBinding(ClickHereCommand,ClickHereExecuted));
}
公共无效ClickHereExecuted(对象发件人,ExecutedRoutedEventArgs E)
{
System.Windows.MessageBox.Show(你好);
}
}
用户控件XAML
<用户控件X:类=CommandBindingTest.UserControl1
的xmlns =http://schemas.microsoft.com / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:MC =HTTP://schemas.openxmlformats .ORG /标记兼容性/ 2006
的xmlns:D =http://schemas.microsoft.com/expression/blend/2008
MC:可忽略=D
D: DesignHeight =300D:DesignWidth =300X:NAME =根>
<电网的DataContext ={绑定的ElementName =根}>
<按钮内容={结合ButtonContent}命令={结合ClickHereCommand}HEIGHT =25WIDTH =90>< /按钮>
< /网格和GT;
< /用户控件>
用户控件代码隐藏
公共部分类的UserControl1:用户控件,INotifyPropertyChanged的
{
私人字符串_ButtonContent;
公共字符串ButtonContent
{
{返回_ButtonContent; }
组
{
如果(_ButtonContent =价值!)
{
_ButtonContent =价值;
OnPropertyChanged(ButtonContent);
}
}
}
公共静态的RoutedCommand ClickHereCommand {搞定;组; }
公众的UserControl1()
{
的InitializeComponent();
ClickHereCommand =新的RoutedCommand();
CommandBindings.Add(新的CommandBinding(ClickHereCommand,ClickHereExecuted));
ButtonContent =点击这里;
}
公共无效ClickHereExecuted(对象发件人,ExecutedRoutedEventArgs E)
{
System.Windows.MessageBox.Show(你好来自的UserControl1);
}
#地区INotifyPropertyChanged的会员
公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效OnPropertyChanged(字符串名称)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(名)) ;
}
}
#endregion
}
这是最好的解决办法:
<电网的DataContext ={绑定的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型用户控件}}}>
<按钮内容={结合ButtonContent}命令={结合ClickHereCommand}HEIGHT =25WIDTH =90>< /按钮>
< /网格和GT;
其他的解决方案:
您忘记设置的DataContext 的到的的UserControl1 的
公开的UserControl1()
{
的InitializeComponent();
ClickHereCommand =新的RoutedCommand();
CommandBindings.Add(新的CommandBinding(ClickHereCommand,ClickHereExecuted));
ButtonContent =点击这里;
this.DataContext =这一点;
}
和在此之后,你必须删除的的UserControl1 的DataContext的在网格
这:
<电网的DataContext = {结合的ElementName =根}>
<按钮内容={结合ButtonContent}命令={结合ClickHereCommand}HEIGHT =25WIDTH =90>< /按钮>
< /网格和GT;
您必须改成这样:
<网格和GT;
<按钮内容={结合ButtonContent}命令={结合ClickHereCommand}HEIGHT =25WIDTH =90>< /按钮>
< /网格和GT;
解决方案,而设置的DataContext在用户控件:
您必须更改ButtonContent和ClickHereCommand到的DependencyProperty。
公共字符串ButtonContent
{
{返回(串)的GetValue(ButtonContentProperty); }
集合{的SetValue(ButtonContentProperty,值); }
}
公共静态只读的DependencyProperty ButtonContentProperty =
DependencyProperty.Register(ButtonContent的typeof(串),typeof运算(的UserControl1),新UIPropertyMetadata(的String.Empty)) ;
公众的RoutedCommand ClickHereCommand
{
{返回(的RoutedCommand)的GetValue(ClickHereCommandProperty); }
集合{的SetValue(ClickHereCommandProperty,值); }
}
公共静态只读的DependencyProperty ClickHereCommandProperty =
DependencyProperty.Register(ClickHereCommand的typeof(的RoutedCommand),typeof运算(的UserControl1),新UIPropertyMetadata(NULL));
而在的UserControl1
的构造函数:
公开的UserControl1()
{
的InitializeComponent();
ClickHereCommand =新的RoutedCommand();
CommandBindings.Add(新的CommandBinding(ClickHereCommand,ClickHereExecuted));
ButtonContent =点击这里;
//this.DataContext =这一点;
}
In MainWindow the commandbinding works fine.In UserControl1 it doesnt work. Note the datacontext is set correctly as is evidenced by the content of the button which is the result of a binding.
I am not trying to bind the command in the usercontrol to a command in mainwindow or any other such trickery. I am just trying to replicate what I did in MainWindow in UserControl1.
MainWindow XAML
<StackPanel>
<Button Content="Click Here" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
<local:UserControl1></local:UserControl1>
</StackPanel>
MainWindow Code Behind
public partial class MainWindow : Window
{
public static RoutedCommand ClickHereCommand { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
}
public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.MessageBox.Show("hello");
}
}
UserControl XAML
<UserControl x:Class="CommandBindingTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="root">
<Grid DataContext="{Binding ElementName=root}" >
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
</Grid>
</UserControl>
UserControl Code Behind
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
private string _ButtonContent;
public string ButtonContent
{
get { return _ButtonContent; }
set
{
if (_ButtonContent != value)
{
_ButtonContent = value;
OnPropertyChanged("ButtonContent");
}
}
}
public static RoutedCommand ClickHereCommand { get; set; }
public UserControl1()
{
InitializeComponent();
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
ButtonContent = "Click Here";
}
public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.MessageBox.Show("hello from UserControl1");
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
It's the best solution:
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" >
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
</Grid>
Other solutions:
You forgot set DataContext to UserControl1.
public UserControl1()
{
InitializeComponent();
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
ButtonContent = "Click Here";
this.DataContext = this;
}
And after this you must delete in UserControl1 DataContext in Grid.
This:
<Grid DataContext="{Binding ElementName=root}" >
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
</Grid>
you must change to this:
<Grid>
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
</Grid>
Solution without set DataContext in UserControl:
You must change ButtonContent and ClickHereCommand to DependencyProperty.
public string ButtonContent
{
get { return (string)GetValue(ButtonContentProperty); }
set { SetValue(ButtonContentProperty, value); }
}
public static readonly DependencyProperty ButtonContentProperty =
DependencyProperty.Register("ButtonContent", typeof(string), typeof(UserControl1), new UIPropertyMetadata(string.Empty));
public RoutedCommand ClickHereCommand
{
get { return (RoutedCommand)GetValue(ClickHereCommandProperty); }
set { SetValue(ClickHereCommandProperty, value); }
}
public static readonly DependencyProperty ClickHereCommandProperty =
DependencyProperty.Register("ClickHereCommand", typeof(RoutedCommand), typeof(UserControl1), new UIPropertyMetadata(null));
And in ctor of UserControl1
:
public UserControl1()
{
InitializeComponent();
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
ButtonContent = "Click Here";
//this.DataContext = this;
}
这篇关于如何使用命令绑定的用户控件在WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!