在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(preferencesPanel)内,有一个按钮,单击该按钮时需要关闭/折叠用户控件。我计划使用Visibility.collapsed做到这一点。我假设我需要使用路由命令,因为该按钮在用户控件内,但是需要在包含控件的页面内调用这些动作?我还是路由命令的新手,我认为这是正确的方法。我只是不确定如何单击用户控件中的按钮,并让它修改或执行会影响页面(此控件所在的页面)的更改方式或对该部分影响页面中其他元素的命令?例如,当在用户控件中单击按钮时,我希望将用户控件的可见性设置为折叠。我也想让主页中网格列之一的宽度重新调整大小。我过去使用该页面后面的代码来完成此操作,但是我试图将其中一些代码分开,我认为路由命令会成为方法吗? 我将不胜感激任何提示。 先感谢您     
已邀请:
标题有点误导,如果我理解正确的话,您是在询问命令而不是路由事件。 这是使用Prism库中的
DelegateCommand<T>
的示例;这恰好是我个人的喜好。 标记:
<Button x:Name=\"MyButton\" Content=\"Btn\" Command=\"{Binding DoSomethingCommand}\"/>
代码隐藏*或ViewModel: (*如果您不使用MVVM,请确保添加
MyButton.DataContext = this;
,以确保按钮可以有效地将数据绑定到您的代码后面)
public DelegateCommand<object> DoSomethingCommand
{
    get 
    { 
        if(mDoSomethingCommand == null)
            mDoSomethingCommand = new DelegateCommand(DoSomething, canDoSomething);
        return mDoSomethingCommand;
    }

private DelegateCommand<object> mDoSomethingCommand;

// here\'s where the command is actually executed
void DoSomething(object o)
{}

// here\'s where the check is made whether the command can actually be executed
// insert your own condition here
bool canDoSomething(object o)
{ return true; }


// here\'s how you can force the command to check whether it can be executed
// typically a reaction for a PropertyChanged event or whatever you like
DoSomethingCommand.RaiseCanExecuteChanged();
传递给上述函数的参数是
CommandParameter
依赖属性(在Prism中,它是一个附加属性,如果内存对我来说是Command属性)。 设置后,您可以将选择的值传递给要执行的命令。 希望能有所帮助。     

要回复问题请先登录注册