WP7.1中不呈现自定义控件

时间:2012-03-21 04:54:36

标签: windows-phone-7

我想在WP7.1中创建WatermarkTextBox,我按照链接

http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control

但是,自定义控件不会在MainPage中呈现。我做错了什么?

WaterMarkTextBox.cs

public class WaterMarkTextBox : TextBox
    {
        public WaterMarkTextBox()
        {
            DefaultStyleKey = typeof(WaterMarkTextBox);
        }
    }

WaterMarkTextBoxStyle.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
    xmlns:local="clr-namespace:MyCustomControls">
    <Style TargetType="local:WaterMarkTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:WaterMarkTextBox">
                    <Grid>
                        <Border>
                            <ContentControl x:Name="ContentTextBox" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

样品:

包括以下

  

的xmlns:本地= “CLR-名称空间:MyCustomControls;装配= MyCustomControls”

     

1 个答案:

答案 0 :(得分:0)

解决方案:我添加了BorderBrush,BorderThickness和Background颜色。现在工作正常。

    <ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:customcontrols">
<Style TargetType="local:WaterTextBox">
    <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:WaterTextBox">
                <Grid Background="Transparent">
                    <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">  
                            <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>                          
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

谢谢。