2013-04-10 55 views
1

我試圖在Windows 8 XAML中創建自定義文本框控件。爲Windows 8商店應用程序創建自定義XAML文本框

我對上我的項目點擊 - >添加 - >新建項目

我再選擇模板化控制和輸入的名字MyTextBox

我已經再發這個類從文本框派生,並添加一個名爲Hello的測試方法。所以現在看起來是這樣的:

public sealed class MyTextBox : TextBox 
{ 
    public MyTextBox() 
    { 
     this.DefaultStyleKey = typeof(MyTextBox); 
    } 

    public void Hello() 
    { 
     //Do something here! 
    } 
} 

在我的項目中的文件也被添加稱爲Generic.xaml有以下樣式:

<Style TargetType="local:MyTextBox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:MyTextBox"> 
       <Border 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}"> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

所以這裏內我嘗試添加支持算法FMP =「文本框」與樣式標籤:

<Style TargetType="local:WatermarkTextBox" BasedOn="TextBox"> 

這是行不通的。

什麼我需要做的創建這個定製文本框,然後我怎麼使用它在我的XAML

這是一個Windows RT所以XAML可能從WPF不同之內。

回答

2

一個最好的文章中,我最喜歡:Building a deployable custom control for XAML Metro style apps

這裏是MSDN示例應用程序:XAML user and custom controls sample

更新1:

<Style TargetType="local:WatermarkTextBox" BasedOn="TextBox">

您不必指定BasedOn屬性。如果您正在開發水印文本框,那麼我會建議您檢查Callisto的水印文本框代碼。

Generic.xaml

WatermarkTextBox.cs

+0

這篇文章雖然不錯,但沒有任何關於從現有的控件派生出來的控件 – Sun 2013-04-10 14:32:18

+0

檢查我更新的答案。 – Xyroid 2013-04-10 14:51:23

+0

感謝您的幫助 – Sun 2013-04-12 14:39:21

0

支持算法FMP值可以通過StaticResources指出像

我有一個NumericOnlyTextBox實現。而不是使它BasedOn文本框我已經添加了一個文本框及其所有依賴項。

在例如:

<Style TargetType="local:NumericOnlyTextBox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:NumericOnlyTextBox"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <StackPanel> 
         <TextBox x:Name="TextBoxPart" 
          Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=EnteredText, Mode=TwoWay}" 
          MaxLength="{TemplateBinding MaxLength}"> 
         </TextBox>        
        </StackPanel> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="IsTabStop" Value="False"></Setter> 
</Style> 
+0

我想你的例子,它是最接近我有我的文本框的工作。我不想直觀地改變任何東西,所以我用ControlTemplate中的所有內容替換:但是當文本框放置在我的XAML中時,沒有任何樣式和數據綁定可以工作。 – Sun 2013-04-10 14:25:47

相關問題