SORU
20 Mayıs 2009, ÇARŞAMBA


Nasıl WPF WPF içsel içsel devralabilir?

Aşağıdaki WPF UserControl aradıDataTypeWholeNumberçalışır.

Şimdi içsel olarak adlandırılan yapmak istiyorumDataTypeDateTimeveDataTypeEmailvb.

Bağımlılık Özellikleri birçok tüm bu kontroller tarafından paylaşılır ve bu nedenle koymak istiyorumbir BaseDataType ortak yöntemlerive bu UserControls her biri bu temel tipinden devralır.

O zaman ancak alırımhata: Kısmi Bildirimi farklı sınıflar olmayabilir.

Nasıl paylaşılan işlevselliği tüm temel sınıf içinde UserControls ile miras uygulayabilir miyim?

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.DataTypes
{
    public partial class DataTypeWholeNumber : BaseDataType
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;

            //defaults
            TheWidth = 200;
        }

        public string TheLabel
        {
            get
            {
                return (string)GetValue(TheLabelProperty);
            }
            set
            {
                SetValue(TheLabelProperty, value);
            }
        }

        public static readonly DependencyProperty TheLabelProperty =
            DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public string TheContent
        {
            get
            {
                return (string)GetValue(TheContentProperty);
            }
            set
            {
                SetValue(TheContentProperty, value);
            }
        }

        public static readonly DependencyProperty TheContentProperty =
            DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public int TheWidth
        {
            get
            {
                return (int)GetValue(TheWidthProperty);
            }
            set
            {
                SetValue(TheWidthProperty, value);
            }
        }

        public static readonly DependencyProperty TheWidthProperty =
            DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());



    }
}

CEVAP
20 Mayıs 2009, ÇARŞAMBA


Xaml ilk etiketi de devralan yeni temel alınan tür için değiştirilmiş olduğundan emin olun

Bu yüzden

<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    >

olur

<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
    >

Bu yüzden, aşağıda yorum ekstra ayrıntıları da dahil olmak üzere tam cevabı şöyle özetleyebilirim:

  • Temel sınıf xaml dosya içermemelidir. (Non-parsiyel) tek cs dosyasında tanımlamak ve doğrudan Usercontrol miras almak tanımlayın.
  • Alt sınıfın temel sınıftan hem xaml (yukarıda gösterildiği gibi) ilk etiketi cs arka planda kodlama dosya ve miras emin olun.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • engineerguy

    engineerguy

    10 Ocak 2010
  • MrDevin521

    MrDevin521

    18 Temmuz 2010
  • Phymec

    Phymec

    18 Temmuz 2009