C# wpf 自定义路由事件

在这里插入图片描述
Main.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <local:UserControl1 x:Name="test"/>
    </Grid>
</Window>

main.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            
            

        }


    }

    public class hh:INotifyPropertyChanged
    {
        public int MyProperty { get; set; }

        public int MyProperty1 { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

StaticRouter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    public static class StaticRouter
    {
        public static readonly RoutedEvent hh = EventManager.RegisterRoutedEvent("hh", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(hh));
    }
}

UserControl1.xaml

<UserControl x:Class="WpfApp1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             x:Name="test1">
    <Grid>
        <DataGrid x:Name="griddatalist"  
                      TabIndex="1"
                      x:FieldModifier="public"
                      ItemsSource="{Binding DataList}"
                      IsReadOnly="True" 
                      MouseDoubleClick="griddatalist_MouseDoubleClick"    
                      SelectionMode="Single" 
                      CanUserDeleteRows="False"
                      CanUserSortColumns="False"
                      CanUserReorderColumns ="False"    
                      CanUserResizeColumns="False"
                      Width="Auto"
                      Margin="6,6,6.4,6"
                        AutoGenerateColumns="False"
                      >
            <DataGrid.Columns>
                <DataGridTextColumn   Header="时间模板"   Binding="{Binding MyProperty}"      IsReadOnly="True"    />
                <DataGridTextColumn   Header="实际进站"  IsReadOnly="True" Width="70" MinWidth="10"   Binding="{Binding    MyProperty1 }"       />
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</UserControl>

UserControl1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public List<hh> DataList { get; set; }
        public UserControl1()
        {
            InitializeComponent();
            this.test1.AddHandler(StaticRouter.hh, new RoutedEventHandler(this.testRoute));
            DataList = new List<hh>() { new hh { MyProperty = 1, MyProperty1 = 2 } };
            this.griddatalist.ItemsSource = DataList;
        }

        public void testRoute(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Source消息源是:" + (e.Source as FrameworkElement).Name + "\r\n"
                         + "OriginalSource 消息源是:" + (e.OriginalSource as hh).MyProperty1);

        }

        private void griddatalist_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Point aP = e.GetPosition(this.griddatalist);
            IInputElement obj = this.griddatalist.InputHitTest(aP);
            DependencyObject target = obj as DependencyObject;

            while (target != null)
            {
                if (target is DataGridCell)
                {
                    var tg = target as DataGridCell;

                    //修改发车时间
                    if (tg.Column.Header.ToString() == "实际进站")
                    {
                        RoutedEventArgs arg = new RoutedEventArgs(StaticRouter.hh, new hh { MyProperty = 123, MyProperty1 = 456 });
                        this.RaiseEvent(arg);
                        break;
                    }
                }
                target = VisualTreeHelper.GetParent(target);
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值