你能否在Silverlight中覆盖控件模板的一部分

mat*_*ser 6 silverlight

是否可以更改或修改控件模板的特定部分,而无需在xaml中重新创建控件的整个控件模板?

例如,我试图摆脱文本框的边框,所以我可以把一个带圆角的基本搜索框放在一起(例如下面的xaml).将borderthickness设置为0可以正常工作,直到您将鼠标悬停在文本框上并且他们添加到控件中的伪边框闪烁.如果我查看文本框的controltemplate,我甚至可以看到视觉状态被命名,但无法想到如何禁用它.

如果不重写TextBox的控件模板,我将如何停止Visual State Manager在TextBox上触发鼠标效果?

<Border Background="White" CornerRadius="10" VerticalAlignment="Center" HorizontalAlignment="Center" BorderThickness="3" BorderBrush="#88000000">
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Margin="5,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="16" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Path Height="13" Width="14" Stretch="Fill" Stroke="#FF000000" StrokeThickness="2" Data="M9.5,5 C9.5,7.4852815 7.4852815,9.5 5,9.5 C2.5147185,9.5 0.5,7.4852815 0.5,5 C0.5,2.5147185 2.5147185,0.5 5,0.5 C7.4852815,0.5 9.5,2.5147185 9.5,5 z M8.5,8.4999971 L13.5,12.499997" />
            <TextBox GotFocus="TextBox_GotFocus" Background="Transparent" Grid.Column="1" BorderThickness="0" Text="I am searchtext" Margin="5,0,5,0" HorizontalAlignment="Stretch" />
    </Grid>
</Border>
Run Code Online (Sandbox Code Playgroud)

mat*_*ser 5

通过继承控件并覆盖OnApplyTemplate,我找到了一种方法.它并不理想,但我认为它比复制整个控件模板更好.以下是创建无边框文本框的示例,通过始终清除故事板,实际上禁用鼠标覆盖可视状态:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace SilverlightTestApplication
{
    public class BorderlessTextBox  : TextBox
    {
        public BorderlessTextBox()
        {
            BorderThickness = new System.Windows.Thickness(0);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //Get the mouse over animation in the control template
            var MouseOverVisualState = GetTemplateChild("MouseOver") as VisualState;

            if (MouseOverVisualState == null)
                return;

            //get rid of the storyboard it uses, so the mouse over does nothing
            MouseOverVisualState.Storyboard = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)