WPF UserControls - 在 UserControl 内的按钮上设置 .Command 属性
我有一个包含按钮和其他一些控件的 UserControl:
<UserControl>
<StackPanel>
<Button x:Name="button" />
...
</StackPanel>
</UserControl>
当我创建该控件的新实例时,我想获取按钮的 Command 属性:
<my:GreatUserControl TheButton.Command="{Binding SomeCommandHere}">
</my:GreatUserControl>
当然,“TheButton.Command”功能不起作用。
所以我的问题是:使用 XAML,如何设置用户控件内按钮的 .Command 属性?
I've got a UserControl that contains a button and some other controls:
<UserControl>
<StackPanel>
<Button x:Name="button" />
...
</StackPanel>
</UserControl>
When I create a new instance of that control, I want to get at the Button's Command property:
<my:GreatUserControl TheButton.Command="{Binding SomeCommandHere}">
</my:GreatUserControl>
Of course, the "TheButton.Command" thing doesn't work.
So my question is: Using XAML, how can I set the .Command property of the button inside my user control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将依赖属性添加到 UserControl 并将按钮的 Command 属性绑定到该属性。
因此,在您的 GreatUserControl 中:
在您的 GreatUserControl 的 XAML 中:
因此,您的按钮绑定到 UserControl 本身的命令。现在您可以在父窗口中进行设置:
Add a dependency property to your UserControl and bind the button's Command property to that.
So in your GreatUserControl:
And in your GreatUserControl's XAML:
So your button binds to the command on the UserControl itself. Now you can set that in your parent window: