WPF UserControl with generic code-behind

后端 未结 3 824
余生分开走
余生分开走 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:06

    Unfortunately XAML does not support generic code behind, thou you can walk around this.

    See links below:

    http://forums.silverlight.net/forums/p/29051/197576.aspx

    Can I specify a generic type in XAML (pre .NET 4 Framework)?

    May be generic controls will be supported natively in future versions of Visual Studuo with XAML 2009.

    0 讨论(0)
  • 2020-12-31 06:11

    Haven't found my solution anywhere else so far. The main difference is, that I have got one .xaml file for the generic user control class and not one for each actual user control.

    GenericUserControl.xaml.cs

    using System.Windows.Controls;
    
    namespace TestGenericUserControl
    {
        public abstract partial class GenericUserControl : UserControl
        {
            // If you use event handlers in GenericUserControl.xaml, you have to define 
            // them here as abstract and implement them in the generic class below, e.g.:
    
            // abstract protected void MouseClick(object sender, MouseButtonEventArgs e);
        }
    
        public class GenericUserControl<T> : GenericUserControl
        {
            // generic properties and stuff
    
            public GenericUserControl()
            {
                InitializeComponent();
            }
        }
    
        // To use the GenericUserControl<T> in XAML, you could define:
        public class GenericUserControlString : GenericUserControl<string> { }
        // and use it in XAML, e.g.:
        // <GenericUserControlString />
        // alternatively you could probably (not sure) define a markup extension to instantiate
        // it directly in XAML
    }
    

    GenericUserControl.xaml

    <UserControl x:Class="TestGenericUserControl.GenericUserControl"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            mc:Ignorable="d">
        <Grid>
            <Label Content="hello" />
        </Grid>
    </UserControl>
    
    0 讨论(0)
  • 2020-12-31 06:20

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

    public class CustomUserControl<T>: UserControl
    { }
    

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

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

    XAML:

    <application:CustomUserControl 
         x:TypeArguments="application:Presenter"
         xmlns:application="clr-namespace:YourApplicationNamespace"
    ...
    

    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)

    0 讨论(0)
提交回复
热议问题