在 Silverlight 用户控件中使用路由事件
在我当前的项目文件中,我有一个用户控件,该控件应用了故事板动画。当单击页面中的按钮时,故事板将启动并基本上以可视方式向用户呈现控件。故事板作为资源驻留在当前页面中
<navigation:Page.Resources>
<Storyboard x:Name="PreferncesOpen">....</Storyboard x:Name="PreferncesOpen">
</navigation:Page.Resources>
在页面中,我有一个按钮,在该按钮上有一个单击事件来启动故事板
private void btnOpenPreferences_Click(object sender, RoutedEventArgs e)
{
preferencesPanel.Visibility = System.Windows.Visibility.Visible;
PreferncesOpen.Begin();
}
在 userControl (首选项面板)中,我有一个按钮,单击该按钮时需要关闭/折叠用户控件。我计划使用 Visibility.collapsed 来做到这一点。我假设我需要使用路由命令,因为按钮位于用户控件内,但需要在包含控件的页面内调用操作?我对路由命令仍然陌生,我认为这是正确的方法。我只是不确定如何单击用户控件中的按钮并让它修改或执行命令,这些命令会影响页面(此控件所在的页面)如何更改或该部分影响页面中的其他元素?例如,当在用户控件中单击按钮时,我希望将用户控件的可见性设置为折叠。我还想调整主页中网格列之一的宽度。我过去曾使用页面后面的代码完成此操作,但我正在尝试分离其中的一些内容,并且我认为路由命令是可行的方法? 我非常感谢任何提示。
先感谢您
within my current project file I have a user control that has a storyboard animation applied to the control. When a button is clicked in the page the storyboard starts and basically visually presents the control to the user. The storyboard resides in the current page as a resource
<navigation:Page.Resources>
<Storyboard x:Name="PreferncesOpen">....</Storyboard x:Name="PreferncesOpen">
</navigation:Page.Resources>
Within the page I have button that I have a click event on that starts the storyboard
private void btnOpenPreferences_Click(object sender, RoutedEventArgs e)
{
preferencesPanel.Visibility = System.Windows.Visibility.Visible;
PreferncesOpen.Begin();
}
Within the userControl (preferencesPanel) I have a button that when clicked needs to close/collapse the user control. I plan to do this using Visibility.collapsed. I assume that I need to use routed commands since the button is within the user control but the actions need to be called within the page that contains the control? I'm still new to routed commands and I assume this is the correct approach. I'm just unsure how to click on a button within the user control and have it modify or execute commands that would impact how the page (in which this control resides) may change or for that part affect other elements within the page? For example when the button is clicked within the user control I would like the visibility of the user control to be set to collapsed. I also would like to have the width of one of the grid columns within the main page re-size. I have done this in the past using the code behind for the page but I am trying to separate some of this and I thought routed commands would be the way to go?
I'd greatly appreciate any tips.
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标题有点误导,如果我理解正确的话,您是在询问命令而不是路由事件。
以下是使用 Prism 库中的
DelegateCommand
的示例;这恰好是我个人的喜好。标记:
代码隐藏*或 ViewModel:
(*如果您不使用 MVVM,请确保添加
MyButton.DataContext = this;
所以您确定该按钮可以有效地将数据绑定到您的代码后面)传递给上述函数的参数是
CommandParameter
依赖属性(在 Prism 中,它是一个附加属性以及 Command 属性,如果内存对我来说是对的)。设置后,您可以将您选择的值传递给您希望执行的命令。
希望有帮助。
The title is a bit misleading, you're asking about commands rather then routed events if I understand you correctly.
Here's an example of using a
DelegateCommand<T>
from the Prism library; It happens to be my personal preference.Markup :
Code-behind* or ViewModel :
(* if you're not using MVVM make sure to add
MyButton.DataContext = this;
so you're sure that the button can databind to your code behind effectively)The argument that's passed to the above function is the
CommandParameter
dependency property (in Prism it's an attached property as well as the Command property if memory serves me right).When it's set, you can pass a value of your choosing to the command that you wish to execute.
Hope that helps.