事件处理程序不适用于继承控件

时间:2014-08-28 10:46:21

标签: c# wpf xaml .net-4.5

我有一个继承自ComboBox的类。如果我在我的控件中使用标准的ComboBox类,则以下事件处理程序可以正常工作:

private void SelectionChanged(object sender, RoutedEventArgs e)

如果我将类型更改为MyComboBox,则启动应用程序会失败,并显示以下消息:

无法绑定到目标方法,因为其签名或安全透明度与委托类型的方式不兼容。

现在我需要将签名更改为

SelectionChanged(object sender, SelectionChangedEventArgs e)

是什么给出了?

编辑:一些示例代码......

MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="wpfApplication2:MyComboBox">
                    <EventSetter Event="SelectionChanged" Handler="SelectionChanged" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>

        <wpfApplication2:MyComboBox Height="23" HorizontalAlignment="Left" Margin="138,145,0,0" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

MainWindow.xaml.cs:

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectionChanged(object sender, RoutedEventArgs e)
        {

        }
    }
}

MyComboBox.cs:

using System.Windows.Controls;

namespace WpfApplication2
{
    public class MyComboBox : ComboBox
    {
    }
}

0 个答案:

没有答案