2014-04-15 53 views
0

我是WPF的新手,我正在編寫一個simlpe WPF自定義控件。以下是我的代碼。在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'類型聲明的部分修飾符;這種類型的其他部分存在申報

+0

那麼錯誤信息是非常明確。你在兩個地方定義了這個類,它在另一個聲明中是局部的。保持其中一個不是兩個。 – Dmitry

+0

我只在一個地方定義。 – user2998181

回答

0

更換

public class CustomControl1 : Control 

有了這個:

public partial class CustomControl1 : Control 
+0

它沒有幫助。我得到了以下錯誤。 錯誤「textbtn.CustomControl1」的分部聲明不能指定 – user2998181

+0

你有錯誤的XAML代碼 – csharpwinphonexaml

+0

不同的基類這必須是一個控制未ResourceDictionary中 – csharpwinphonexaml

相關問題