簡體   English   中英

WPF自定義控件中的添加按鈕

[英]Adding Button in WPF Custom Control

我是WPF的新手,正在編寫simlpe WPF自定義控件。 以下是我的代碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace textbtn
{
    public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }        

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //if (test.Width > 50)
            //    test.Width = 0;
            //else          
            //    test.Width = 100;
        }
    }
}

我的XAML代碼:

<ResourceDictionary
    x:Class="textbtn.CustomControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:textbtn">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Button Content="CButton" Height="23" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Center" Width="75" Click="button1_Click"/>
                            <TextBlock Text="This is a Test" Foreground="Aqua" Background="AntiqueWhite" />
                        </Grid>
                    </Border>                    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我收到以下錯誤。 請幫我解決這個問題。 提前致謝。

錯誤1在聲明類型'textbtn.CustomControl1'時缺少部分修飾符; 存在這種類型的另一部分聲明

更換

public class CustomControl1 : Control

有了這個:

public partial class CustomControl1 : Control

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM