首页 > 其他分享 >WPF Generic eventhandler for various event

WPF Generic eventhandler for various event

时间:2024-09-06 16:25:46浏览次数:12  
标签:eventhandler AddedItems just SelectionChangedEvent Generic sce GenericHandler WP

public MainWindow()
{
    InitializeComponent();
    this.AddHandler(ListBox.SelectionChangedEvent,
        new SelectionChangedEventHandler(GenericHandler));

    this.AddHandler(Button.ClickEvent, 
        new RoutedEventHandler(GenericHandler));
}        

void GenericHandler(object sender, RoutedEventArgs e)
{
    if(e.RoutedEvent==Button.ClickEvent)
    {
        MessageBox.Show("You just clicked " + e.Source);
    }
    else if(e.RoutedEvent==ListBox.SelectionChangedEvent)
    {
        SelectionChangedEventArgs sce=e as SelectionChangedEventArgs;
        if(sce.AddedItems.Count>0)
        {
            MessageBox.Show("You just selected "+sce.AddedItems[0]);
        }
    }
}

 

<Window x:Class="WpfApp343.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:WpfApp343"        
        SizeToContent="WidthAndHeight"
        Background="OrangeRed"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Label FontWeight="Bold" FontSize="20" Foreground="White">
            WPF 4 Unleashed
        </Label>
        <Label>@ 2010 SMAS Publishing</Label>
        <Label>Installed Chapters:</Label>
        <ListBox>
            <ListBoxItem>Chapter 1</ListBoxItem>
            <ListBoxItem>Chapter 2</ListBoxItem>
        </ListBox>
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center">
            <Button MinWidth="75" Margin="10">Help</Button>
            <Button MinWidth="75" Margin="10">OK</Button>
        </StackPanel>
        <StatusBar>You have successfully registered this product.</StatusBar>
    </StackPanel>
</Window>

 

 

 

 

 

标签:eventhandler,AddedItems,just,SelectionChangedEvent,Generic,sce,GenericHandler,WP
From: https://www.cnblogs.com/Fred1987/p/18400493

相关文章

  • [Typescript] Handle Errors with a Generic Result Type
    Considerthis Result type:typeResult<TResult,TError>=|{success:true;data:TResult;}|{success:false;error:TError;};The Result typehastwotypeparametersfor TResult and TError.Itreturnsadisc......
  • .NET 8 + WPF 企业级工作流系统
    合集-.NET开源工具(11) 1..NET开源快捷的数据库文档查询和生成工具07-312..NET结果与错误处理利器FluentResults08-013..NET+WPF桌面快速启动工具GeekDesk08-194.Gradio.NET支持.NET8简化Web应用开发08-265..NET开源实时监控系统-WatchDog08-276.实用接地......
  • WPF ListBox ListBoxItem Selected background style
    <Windowx:Class="WpfApp340.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • 【转】[C#][WPF] 旋转的播放按钮
    转自:http://www.dmskin.com运行后播放按钮就会一直旋转,而暂停按钮不动。<Windowx:Class="WPF.Views.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quo......
  • .NET 8 + WPF 企业级工作流系统
    前言推荐一款基于.NET8、WPF、Prism.DryIoc、MVVM设计模式、Blazor以及MySQL数据库构建的企业级工作流系统的WPF客户端框架-AIStudio.Wpf.AClient6.0。项目介绍框架采用了Prism框架来实现MVVM模式,不仅简化了MVVM的典型应用场景,还充分利用了依赖注入(DI)、消息传递以及容......
  • DevExpress WPF中文教程:如何解决排序、过滤遇到的常见问题?(一)
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • WPF Customize Button ControlTemplate TextBlock
    //xaml<UserControlx:Class="WpfApp332.BtnTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="......
  • WPF UniformGrid contain children auto resize
    //xaml<Windowx:Class="WpfApp332.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF性能优化之UI虚拟化
    @目录前言一、VirtualizingStackPanel1.1虚拟化功能介绍1、在Window中添加一个ListBox控件。2、在设计视图中用鼠标选中ListBox控件并右健依次单击“编辑其他模板”-“编辑项的布局模板”-“编辑副本”。3、查看生成的模板代码。1.2虚拟化参数介绍二、CustomVirtualizingPanel2.1......
  • WPF性能优化之UI虚拟化
    前言相信很多WPF开发者都碰到过这种情况,当在一个ItemsControl(或继承自ItemsControl)控件中绑定一个集合的时候,如果集合中的条目过多,那么界面就会变得卡顿甚至停止响应,特别是在容器或窗口大小发生改变时,界面的渲染就会给人一种慢半拍的感觉,体验感非常差,这时我们就可以用虚拟化技术......