代码非标准,仅供参考。
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>