3

我有一个简单的问题,但我是 Silverlight 的新手。

我有一个透明的文本框,但是当我获得焦点时,文本框背景会变成白色。

怎么解决?

在此处输入图像描述 在此处输入图像描述

4

4 回答 4

3

您只需要在 VisualStateManager 中为默认控件模板编辑“FocusedState”,或者在资源字典或 UserControl.Resources 等中提供您自己的类似下面提供的模板。

以下是如何将下面的样式模板应用到您的 TextBox 实例

<TextBox Style="{StaticResource YourCustomTextBoxStyle}/>

这是一个默认的 WP7 文本框样式模板,其适当的位置已调整...

<Style x:Key="YourCustomTextBoxStyle" TargetType="TextBox">  
            <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>  
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>  
            <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>  
            <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>  
            <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>  
            <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>  
            <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>  
            <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>  
            <Setter Property="Padding" Value="2"/>  
            <Setter Property="Template">  
                <Setter.Value> 
                    <ControlTemplate TargetType="TextBox">  
                        <Grid Background="Transparent">  
                            <VisualStateManager.VisualStateGroups> 
                                <VisualStateGroup x:Name="CommonStates">  
                                    <VisualState x:Name="Normal"/>  
                                    <VisualState x:Name="MouseOver"/>  
                                    <VisualState x:Name="Disabled">  
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0">  
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Collapsed</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0">  
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                    <VisualState x:Name="ReadOnly">  
                                        <Storyboard> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0">  
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Collapsed</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0">  
                                                    <DiscreteObjectKeyFrame.Value> 
                                                        <Visibility>Visible</Visibility> 
                                                    </DiscreteObjectKeyFrame.Value> 
                                                </DiscreteObjectKeyFrame> 
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>  
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>  
                                            </ObjectAnimationUsingKeyFrames> 
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DisabledOrReadonlyContent">  
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>  
                                            </ObjectAnimationUsingKeyFrames> 
                                        </Storyboard> 
                                    </VisualState> 
                                </VisualStateGroup> 
                                <VisualStateGroup x:Name="FocusStates">  
                                    <VisualState x:Name="Focused"/>  <!-- *** Right here is your culprit, I just ripped out the FocusedState Storyboard so it doesnt do anything when focused. *** -->

                                    <VisualState x:Name="Unfocused"/>  
                                </VisualStateGroup> 
                            </VisualStateManager.VisualStateGroups> 
                            <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">  
                                <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>  
                            </Border> 
                            <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed">  
                                <TextBox x:Name="DisabledOrReadonlyContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}" SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}" Template="{StaticResource PhoneDisabledTextBoxTemplate}"/>  
                            </Border> 
                        </Grid> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 

默认情况下,您还可以使用BasedOn Values将相同的模板应用于所有 TextBox 控件。

还有其他方法可以做到这一点,但这是开始学习基础知识的好地方。希望能帮助到你。

于 2012-07-30T19:38:23.917 回答
2

这更容易!

    private void TextBox1_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Background = new SolidColorBrush(Colors.Transparent);
        tb.BorderBrush = new SolidColorBrush(Colors.Transparent);
        tb.SelectionBackground = new SolidColorBrush(Colors.Transparent);
    }

    private void TextBox1_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Background = new SolidColorBrush(Colors.Transparent);
        tb.BorderBrush = new SolidColorBrush(Colors.Transparent);
        tb.SelectionBackground = new SolidColorBrush(Colors.Transparent);
    }

而且效果很好!!

于 2012-07-30T19:41:25.110 回答
0

您所描述的是一种视觉状态。

当文本框获得焦点时,它会在内部触发视觉状态更改,在这种情况下包括白色背景。

看起来您正在使用 WP7,因此您可以从几个地方开始学习控制模板、样式以及如何更改它们。

首先看看这篇关于控制模板的文章。

您会发现任何与 Silverlight 4 相关的内容都是相关的。

其次,获得一份 Expression Blend for Windows Phone 的副本。启动一个新的 WP7 项目,将一个 TextBox 拖到您的设计图面上,右键单击 TextBox 并选择 Edit Template。然后你会开始看到改变所说的视觉状态是多么容易。

于 2012-07-30T18:28:37.933 回答
0

看起来您想要单击以编辑样式。Click-to-edit 控件中有一个带有 xaml 代码的 silverlight 示例- 怎么做?邮政。

于 2012-07-30T18:34:11.837 回答