2011-12-13 51 views
0

我在我的wpf TextBox上使用以下樣式。TextBox在樣式上出現故障

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Background" Value="White" /> 
    <Setter Property="Foreground" Value="Black"/> 
    <Setter Property="Padding" Value="2"/> 
    <Setter Property="BorderBrush" Value="Gray"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Grid x:Name="Root"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualStateGroup.Transitions> 
           <VisualTransition GeneratedDuration="0:0:0.3"> 
            <VisualTransition.GeneratedEasingFunction> 
             <QuarticEase EasingMode="EaseOut"/> 
            </VisualTransition.GeneratedEasingFunction> 
           </VisualTransition> 
          </VisualStateGroup.Transitions> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ColorAnimation Duration="0" To="DarkGray" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Border" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"/> 
          <VisualState x:Name="ReadOnly"/> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <ColorAnimation Duration="0" To="Black" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Border" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused"/> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="ValidationStates"> 
          <VisualState x:Name="Valid"/> 
          <VisualState x:Name="InvalidUnfocused"/> 
          <VisualState x:Name="InvalidFocused"/> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="Border" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="1" BorderBrush="Gray"> 
         <ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" VerticalContentAlignment="Center" Padding="5,0,0,0" VerticalAlignment="Center" Margin="0,0,22,0"/> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

關於使用此樣式,文本框停止工作。如果我點擊文本框,鼠標指針消失,則不顯示焦點或文本。這種風格有什麼問題?

回答

1

重命名你的滾動框,它會工作

<ScrollViewer x:Name="PART_ContentHost" BorderThickness="0" IsTabStop="False" VerticalContentAlignment="Center" Padding="5,0,0,0" VerticalAlignment="Center" Margin="0,0,22,0"/> 

希望這有助於

0

看來你的Xaml沒有提供與鍵盤/鼠標交互的地方。我不能確定產品總數你想設計什麼,但如果你把你的ScrollViewer內的一個文本框,你的模板將停止表現出「故障」你所描述的...

<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Gray" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1"> 
    <ScrollViewer x:Name="ContentElement" Margin="0,0,22,0" Padding="5,0,0,0" BorderThickness="0" IsTabStop="False" VerticalAlignment="Center" VerticalContentAlignment="Center"> 
     <TextBox /> 
    </ScrollViewer> 
</Border> 

此片段展示了一個修改到ScrollViewer包含TextBox的Xaml。 MouseDown事件將焦點置於控件上,並按預期發生鍵盤交互。