在开发WP7中,在处理TextBox控件的TextChanged事件响应时发现,每次TextBox内容变化后,总会触发2次事件,很郁闷。
后来查询一些资料,有人说是微软的bug,是TextBox默认模板的问题,可以通过自定义模板解决。
自己用Blend尝试了下,发现确实是在默认模板里又嵌套了一个TextBox,原因不知,将其注释掉,问题解决。
嵌套的TextBox如代码中86行注释掉的部分。
不知道微软这样的设计是出于什么目的。
1 <!--自定义TextBox样式-->
2 <Style x:Key="TextBoxStyle1" TargetType="TextBox">
3 <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
4 <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
5 <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
6 <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
7 <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
8 <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
9 <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
10 <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
11 <Setter Property="Padding" Value="2"/>
12 <Setter Property="Template">
13 <Setter.Value>
14 <ControlTemplate TargetType="TextBox">
15 <Grid Background="Transparent" Margin="0,0,3,0">
16 <VisualStateManager.VisualStateGroups>
17 <VisualStateGroup x:Name="CommonStates">
18 <VisualState x:Name="Normal"/>
19 <VisualState x:Name="MouseOver"/>
20 <VisualState x:Name="Disabled">
21 <Storyboard>
22 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
23 <DiscreteObjectKeyFrame KeyTime="0">
24 <DiscreteObjectKeyFrame.Value>
25 <Visibility>Collapsed</Visibility>
26 </DiscreteObjectKeyFrame.Value>
27 </DiscreteObjectKeyFrame>
28 </ObjectAnimationUsingKeyFrames>
29 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">
30 <DiscreteObjectKeyFrame KeyTime="0">
31 <DiscreteObjectKeyFrame.Value>
32 <Visibility>Visible</Visibility>
33 </DiscreteObjectKeyFrame.Value>
34 </DiscreteObjectKeyFrame>
35 </ObjectAnimationUsingKeyFrames>
36 </Storyboard>
37 </VisualState>
38 <VisualState x:Name="ReadOnly">
39 <Storyboard>
40 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
41 <DiscreteObjectKeyFrame KeyTime="0">
42 <DiscreteObjectKeyFrame.Value>
43 <Visibility>Visible</Visibility>
44 <!--<Visibility>Collapsed</Visibility>-->
45 </DiscreteObjectKeyFrame.Value>
46 </DiscreteObjectKeyFrame>
47 </ObjectAnimationUsingKeyFrames>
48 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">
49 <DiscreteObjectKeyFrame KeyTime="0">
50 <DiscreteObjectKeyFrame.Value>
51 <Visibility>Visible</Visibility>
52 </DiscreteObjectKeyFrame.Value>
53 </DiscreteObjectKeyFrame>
54 </ObjectAnimationUsingKeyFrames>
55
56 <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="DisabledOrReadonlyBorder">
57 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
58 </ObjectAnimationUsingKeyFrames>
59 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="DisabledOrReadonlyBorder">
60 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
61 </ObjectAnimationUsingKeyFrames>
62 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DisabledOrReadonlyContent">
63 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>
64 </ObjectAnimationUsingKeyFrames>-->
65 </Storyboard>
66 </VisualState>
67 </VisualStateGroup>
68 <VisualStateGroup x:Name="FocusStates">
69 <VisualState x:Name="Focused">
70 <Storyboard>
71 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder">
72 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>
73 </ObjectAnimationUsingKeyFrames>
74 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder">
75 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>
76 </ObjectAnimationUsingKeyFrames>
77 </Storyboard>
78 </VisualState>
79 <VisualState x:Name="Unfocused"/>
80 </VisualStateGroup>
81 </VisualStateManager.VisualStateGroups>
82 <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
83 <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="-3" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
84 </Border>
85 <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed">
86 <!--<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}" />-->
87 </Border>
88 </Grid>
89 </ControlTemplate>
90 </Setter.Value>
91 </Setter>
92 </Style>