2010-09-28 24 views
9

Geride bu kodu vardır:WPF UserControl jenerik kod arkasında

CustomUserControl.xaml.cs

namespace MyProject 
{ 
    public partial class CustomUserControl<T> : UserControl 
    { 
     ... 
    } 
} 

ve bu xaml:

CustomUserControl.xaml

<UserControl x:Class="MyProject.CustomUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
<Grid> 

</Grid> 

x: Class = "MyProject.CustomUserControl", kod arkasındaki genel sınıf tanımlamasıyla eşleşmediğinden çalışmaz. Bu işi yapmanın bir yolu var mı?

cevap

8

XAML ile Görsel Studuo gelecekteki sürümlerinde doğal desteklenecektir:

aşağıdaki bağlantılara göz atın XAML dosyası olmadan kod "arkasındaki" dosya:

ve 0 parametre olarak belirli bir sınıf sağlamasının türetmek daha:

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

XAML:

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

Ne yazık ki, Visual Studio tasarımcı Visual Studio 2012 tarihine kadar bu tür Generics'i desteklemediğini görünüyor Güncelleştirme 2 (bkz. https://stackoverflow.com/a/15110115/355438)

4

Çözümüm şu ana kadar başka bir yerde bulunmadı. Ana fark, gerçek kullanıcı denetimi sınıfı için bir .xaml dosyası var ve her gerçek kullanıcı denetimi için bir değil.

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

ben tür ne yaptığınızı görüyorum ama XAML mimarisinin en iyisi değilim. Denetimin neye benzemesi gerektiğini nasıl/nerede belirlersiniz? Tüm bu kodu bir test projesine kopyaladım, ancak yapamayacağım: neye benzemesi gerektiğini, MainWindow.xaml'de bu 'UserControl 'işlevini nasıl kullanacağınızı ve buna veriyi nasıl bağlayacağınızı (ör. 'MyGeneric (Of T)' '' '' '' '' '' '' i '' '' i '' '' ile bağlar. –

+0

@Zach Sizin için küçük bir örnek yazdım. [Bu] bir göz atın (https://github.com/timmi-on-rails/GenericUserControlWPF). – Tom