2012-06-15 37 views
1

我使用控件模板重新设置了文本框。样式工作得很好,但我现在不能在文本框中更改文本框后以编程方式从文本框中获取文本值。无法从控制模板获取文本值

这里是我使用的样式:

 <TextBox 
     x:Name="KernTextBox" 
     Style="{StaticResource MediumTextBoxStyle}" 
     TextWrapping="Wrap" 
     MinLines="1" 
     MaxLines="5" 
     VerticalScrollBarVisibility="Auto" 
     TextChanged="TextChanged" 
     /> 

这里是行不通的。

get { return KernTextBox.Text; } 

这里的风格:

<Style x:Key="MediumTextBoxStyle" TargetType="TextBox"> 
    <Setter Property="FontSize" Value="18" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="TextBox"> 
       <Grid> 

        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver" /> 
          <VisualState x:Name="ReadOnly"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonRectangle" 
             Storyboard.TargetProperty="Fill"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource BackgroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonRectangle" 
             Storyboard.TargetProperty="Fill"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource BackgroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonText" 
             Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource ForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 

         </VisualStateGroup> 

         <VisualStateGroup x:Name="CheckStates"> 
          <VisualState x:Name="Checked" /> 
          <VisualState x:Name="Unchecked"/> 
         </VisualStateGroup> 

         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused" /> 
          <VisualState x:Name="Unfocused"/> 
         </VisualStateGroup> 

        </VisualStateManager.VisualStateGroups> 

        <Rectangle 
         x:Name="ButtonRectangle" 
         Stroke="Transparent" 
         UseLayoutRounding="False" 
         Fill="{StaticResource BackgroundBrush}"> 
        </Rectangle> 

        <TextBox 
         x:Name="ButtonText" 
         Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" 
         FontSize="{TemplateBinding FontSize}" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Center" 
         Margin="2 0" 
         TextWrapping="{TemplateBinding TextWrapping}" 
         MinLines="{TemplateBinding MinLines}" 
         MaxLines="{TemplateBinding MaxLines}" 
         VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}" 
         Padding="{TemplateBinding Padding}" 
         Foreground="{StaticResource ForegroundBrush}" 
         Background="{StaticResource BackgroundBrush}" 
         > 
        </TextBox> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

编辑:

最后的工作作风:

<Style 
    x:Key="TextBoxStyle" 
    TargetType="{x:Type TextBox}"> 
    <Setter 
     Property="Foreground" 
     Value="{StaticResource ForegroundBrush}" /> 
    <Setter 
     Property="FontSize" 
     Value="18" /> 
    <Setter 
     Property="CaretBrush" 
     Value="{StaticResource ForegroundBrush}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Border 
        Name="Border" 
        Background="{StaticResource BackgroundBrush}" 
        BorderBrush="{StaticResource ForegroundBrush}" 
        BorderThickness="1" > 
        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/> 
         <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/> 
        </Trigger> 
        <Trigger Property="IsReadOnly" Value="True"> 
         <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/> 
         <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

1

有,为什么你把一个文本框里面的文本框的理由?检出文本框的原始样式和template,然后从中对其进行修改。我的猜测是,文本框在内部使用TextBoxView(隐藏在滚动查看器中),并且Text属性未通过TemplateBinding绑定,而是在这些内部类之间进行计算。所以也许这就是为什么你不能将你的文本属性绑定到控件模板中的文本框文本的原因。

+0

如果我想围绕每个文本框设计风格,但不必设计完整的文本框,我也会完成这里所做的工作。 – Andy

+0

@安好,依然不是个好主意。用户控件怎么样?或者,只需采取原始样式并对其进行修改,普通的文本框控件模板非常短。但将一个文本框放入一个文本框是一个非常奇怪的“黑客”imho。 – dowhilefor

+0

我接受了您的建议,并重新编写了删除文本框的样式。现在都在工作。我已将样式添加到我的问题中。 –

1

在你的风格中,当你将内部Text属性绑定到Template Text属性时,添加更新源触发器PropertyChanged,即使你没有失去焦点,你也会得到文本更新。

Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

你的造型文本框的方法是不太工作的原因是因为在默认情况下,直到焦点从控件中删除文本属性不会更新它的绑定属性。我猜你正在尝试使用TextChanged事件中的Text属性,该事件在那时并没有从TextBox中移除焦点。

另外我建议你的文本绑定将强制属性更新每次文本更改,并会在TextChanged事件引发之前执行它。