最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Custom Control Partial declarations of 'DigitTextBox' must not specify different base classes - Stack Overf

programmeradmin3浏览0评论

I'm building a Custom Control. Added an event to the xaml. Compiler complained, I added an x:Class entry. Then it complained that my class needed a partial verb. And now it's complaining: "Partial declarations of 'DigitTextBox' must not specify different base classes"

<ResourceDictionary
    xmlns=";
    xmlns:x=";
    x:Class="DigitEntryControl.DigitTextBox"
    xmlns:local="clr-namespace:DigitEntryControl">
    <ControlTemplate x:Key="DigitMode" TargetType="{x:Type local:DigitTextBox}">
        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
            <Canvas PreviewKeyDown="DigitBox_PreviewKeyDown">
                <TextBlock Text="{Binding SolidText}" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Canvas>
        </Border>
    </ControlTemplate>
    <ControlTemplate x:Key="HintMode" TargetType="{x:Type local:DigitTextBox}">
        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
            <UniformGrid Visibility="{Binding TriSelect}" Columns="3"  PreviewKeyDown="DigitBox_PreviewKeyDown">
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
            </UniformGrid>
        </Border>
    </ControlTemplate>
    <Style TargetType="{x:Type local:DigitTextBox}">
        <Setter Property="Template" Value="{StaticResource HintMode}"/>
        <Style.Triggers>
            <Trigger Property="IsHintMode" Value="false">
                <Setter Property="Template" Value="{StaticResource DigitMode}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

The full CS file is a bit long I've only included parts I thought were relevant.

namespace DigitEntryControl
{
    public partial class DigitTextBox : Control, INotifyPropertyChanged
    {
        static DigitTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(typeof(DigitTextBox)));
            BackgroundProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(Brushes.White));
            BorderBrushProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(Brushes.Black));
            BorderThicknessProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(new Thickness(0.5)));
            WidthProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(propertyChangedCallback: HeightWidthPropertyChanged));
            HeightProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(propertyChangedCallback: HeightWidthPropertyChanged));            
        }
    }
}

The other answers for this type of error mostly deal with creating a UserControl. I tried applying the changes suggested for a UserControl, just more errors.

I'm building a Custom Control. Added an event to the xaml. Compiler complained, I added an x:Class entry. Then it complained that my class needed a partial verb. And now it's complaining: "Partial declarations of 'DigitTextBox' must not specify different base classes"

<ResourceDictionary
    xmlns="http://schemas.microsoft/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft/winfx/2006/xaml"
    x:Class="DigitEntryControl.DigitTextBox"
    xmlns:local="clr-namespace:DigitEntryControl">
    <ControlTemplate x:Key="DigitMode" TargetType="{x:Type local:DigitTextBox}">
        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
            <Canvas PreviewKeyDown="DigitBox_PreviewKeyDown">
                <TextBlock Text="{Binding SolidText}" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Canvas>
        </Border>
    </ControlTemplate>
    <ControlTemplate x:Key="HintMode" TargetType="{x:Type local:DigitTextBox}">
        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
            <UniformGrid Visibility="{Binding TriSelect}" Columns="3"  PreviewKeyDown="DigitBox_PreviewKeyDown">
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
            </UniformGrid>
        </Border>
    </ControlTemplate>
    <Style TargetType="{x:Type local:DigitTextBox}">
        <Setter Property="Template" Value="{StaticResource HintMode}"/>
        <Style.Triggers>
            <Trigger Property="IsHintMode" Value="false">
                <Setter Property="Template" Value="{StaticResource DigitMode}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

The full CS file is a bit long I've only included parts I thought were relevant.

namespace DigitEntryControl
{
    public partial class DigitTextBox : Control, INotifyPropertyChanged
    {
        static DigitTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(typeof(DigitTextBox)));
            BackgroundProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(Brushes.White));
            BorderBrushProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(Brushes.Black));
            BorderThicknessProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(new Thickness(0.5)));
            WidthProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(propertyChangedCallback: HeightWidthPropertyChanged));
            HeightProperty.OverrideMetadata(typeof(DigitTextBox), new FrameworkPropertyMetadata(propertyChangedCallback: HeightWidthPropertyChanged));            
        }
    }
}

The other answers for this type of error mostly deal with creating a UserControl. I tried applying the changes suggested for a UserControl, just more errors.

Share asked Mar 6 at 20:40 NottocNottoc 1037 bronze badges 4
  • Where is your other partial class part? How can we see your problem without that part? Indeed, based classes cannot be different. But interfaces can be, because this is a week form of multiple inheritance. The rule is: you don't need to repeat base classes in all parts. The base classes/interfaces from all parts are logically merged as if they were in one class definition. From this information, you will be able to fix your problem. – Sergey A Kryukov Commented Mar 7 at 3:18
  • The XAML you are showing appears to be Themes\Generic.xaml. There you do not add an x:Class declaration, and you do not specify partial on a custom control class. Please clarify what the original problem was. You may want to attach the event handler in code, as shown here: stackoverflow/q/23610581/1136211 – Clemens Commented Mar 7 at 7:45
  • @Clemens You are correct, the XAML is from Generic.xaml. The original problem, as I stated was that I added an event (See on the Canvas and the UniformGrid each has a PreviewKeyDown. As I said this led to a daisy chain of errors. – Nottoc Commented Mar 7 at 15:22
  • Take a look at the linked Q&A. – Clemens Commented Mar 7 at 15:29
Add a comment  | 

1 Answer 1

Reset to default 0

Instead of attaching an event handler in a ControlTemplate in the default control Style, you can attach an event handler in code behind in the OnApplyTemplate method, e.g. as shown here: How to get events handlers for custom controls in Generic.Xaml

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论