C# wpf 自定义路由事件
程序员文章站
2022-06-07 13:52:24
...
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);
}
}
}
}
上一篇: iOS金额输入限制