在 Silverlight 用户控件中使用路由事件

发布于 2024-11-02 07:37:17 字数 888 浏览 7 评论 0原文

在我当前的项目文件中,我有一个用户控件,该控件应用了故事板动画。当单击页面中的按钮时,故事板将启动并基本上以可视方式向用户呈现控件。故事板作为资源驻留在当前页面中

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2024-11-09 07:37:17

标题有点误导,如果我理解正确的话,您是在询问命令而不是路由事件。

以下是使用 Prism 库中的 DelegateCommand 的示例;这恰好是我个人的喜好。

标记:

<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 属性,如果内存对我来说是对的)。
设置后,您可以将您选择的值传递给您希望执行的命令。

希望有帮助。

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 :

<Button x:Name="MyButton" Content="Btn" Command="{Binding DoSomethingCommand}"/>

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)

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();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文