小编Igo*_*ous的帖子

通过Interactivity.EventTrigger捕获自定义事件

我有一个简单的用户控件与事件:

using System.Windows;

namespace TriggersTest
{
    public partial class MyControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        public static readonly RoutedEvent ButtonPressedEvent =
            EventManager.RegisterRoutedEvent("ButtonPressed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyControl));

        public event RoutedEventHandler ButtonPressed
        {
            add { AddHandler(ButtonPressedEvent, value); }
            remove { RemoveHandler(ButtonPressedEvent, value); }
        }

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs { RoutedEvent = ButtonPressedEvent });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望在另一个视图中捕获此事件:

<Window x:Class="TriggersTest.MainWindow"
        Name="Root"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:TriggersTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MyControl>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="ButtonPressed">
                    <i:InvokeCommandAction Command="{Binding MyCommand, ElementName=Root}" /> …
Run Code Online (Sandbox Code Playgroud)

c# wpf eventtrigger wpf-controls

5
推荐指数
1
解决办法
2080
查看次数

标签 统计

c# ×1

eventtrigger ×1

wpf ×1

wpf-controls ×1