WPF UserControl with generic code-behind

后端 未结 3 874
余生分开走
余生分开走 2020-12-31 05:49

I have this code behind:

CustomUserControl.xaml.cs

namespace MyProject
{
    public partial class CustomUserControl : UserControl
    {
             


        
3条回答
  •  余生分开走
    2020-12-31 06:20

    You can create generic "code-behind" file without XAML-file:

    public class CustomUserControl: UserControl
    { }
    

    and than derive from it providing specific class as a parameter:

    public partial class SpecificUserControl : CustomUserControl
    {
        public SpecificUserControl()
        {
            InitializeComponent();
        }
    }
    

    XAML:

    Unfortunately, it seems that Visual Studio designer doesn't support such generics until Visual Studio 2012 Update 2 (see https://stackoverflow.com/a/15110115/355438)

提交回复
热议问题