2011-12-13 53 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事件将焦点置于控件上,并按预期发生键盘交互。