• 为RadComboBox添加SelectionChanging事件


    代码非标准,仅供参考。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Controls;
    using Telerik.Windows.Controls;
    
    namespace slTest
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                this.combo.SelectionChanging += combo_SelectionChanging;
            }
    
            void combo_SelectionChanging(object sender, SelectionChangingEventArgs e)
            {
                e.Cancel = this.isCancel.IsChecked.Value;
            }
        }
    
        public class Combo : Telerik.Windows.Controls.RadComboBox
        {
            public Combo()
            {
                base.SelectionChanged += Combo_SelectionChanged;
            }
    
            private object _orginalItem { get; set; }
    
            void Combo_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
            {
                if (e.AddedItems.Count != 0)
                {
                    if (this._orginalItem == e.AddedItems[0]) return;
                    if (SelectionChanging != null)
                    {
                        var addedItems = new List<Object>();
                        if (e.AddedItems.Count != 0)
                            addedItems.AddRange(e.AddedItems.Cast<Object>());
                        var removedItems = new List<Object>();
                        if (e.RemovedItems.Count != 0)
                            removedItems.AddRange(e.RemovedItems.Cast<Object>());
    
                        var cancelEventArgs = new SelectionChangingEventArgs(addedItems, removedItems, true);
                        SelectionChanging(sender, cancelEventArgs);
                        if (cancelEventArgs.Cancel)//模拟cancel为true
                        {
                            var removedItem = e.RemovedItems.Count != 0 ? e.RemovedItems[0] : null;
                            this._orginalItem = removedItem;
                            this.SelectedItem = removedItem;
                        }
                    }
                }
            }
    
            public event SelectionChangingEventHandler SelectionChanging;
        }
    
        public delegate void SelectionChangingEventHandler(object sender, SelectionChangingEventArgs e);
    }
    
    
    <UserControl x:Class="slTest.MainPage"
                 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:slTest"
                 xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel Width="300" HorizontalAlignment="Left" Margin="5">
                <local:Combo x:Name="combo">
                    <telerik:RadComboBoxItem>1</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>2</telerik:RadComboBoxItem>
                    <telerik:RadComboBoxItem>3</telerik:RadComboBoxItem>
                </local:Combo>
                <TextBox x:Name="selectedValue"></TextBox>
                <CheckBox x:Name="isCancel"></CheckBox>
                <Button Click="Button_Click">Button</Button>
            </StackPanel>
        </Grid>
    </UserControl>
    
  • 相关阅读:
    对于指定区块div,如何区分区块内的点击 和 区块外的点击?
    broadcom代码中httpd进程启动流程介绍
    一个简单的搜索布局样式
    一种在视频OBJECT标签上放置均分四个区域的框选方法
    JQuery执行DOM批量克隆并插入的提效方法
    DevOps技术路线图
    后端开发技术路线图
    Angular route传参
    Angular使用echarts
    TypeScript Array Remove
  • 原文地址:https://www.cnblogs.com/grj1046/p/4916895.html
Copyright © 2020-2023  润新知