2011-08-31 38 views
1

我正在做的是让一个多边形可见,如果它旁边的stackedpanel中的文本框有焦点。基本上,它将成为文本框所关注的指标。这将应用于多个文本框,所以我想使用样式将其设置为通用。XAML:我可以在堆叠面板中获得“下一个控件”并绑定到它的属性吗?

<StackPanel Visibility="{Binding showOpCode}" Margin="0,2" Orientation="Horizontal" > 
      <TextBlock Width="212" VerticalAlignment="Center">Operation Code:</TextBlock> 
      <Polygon Width="29" Points="14,8 14,21 28,14.5" Fill="Gray" Stroke="DarkGray"/> 
      <TextBox HorizontalContentAlignment="Right" 
        Name="txtOpCode" VerticalAlignment="Bottom" Width="122" Text="0" 
        Style="{StaticResource normalTextBoxStyle}" /> 
     </StackPanel> 

回答

1

你最好是建立一个包含多边形的自定义文本框样式。您可以从here获取默认样式。

只需将文本框和相关资源的默认样式,然后将您的多边形添加到左侧。

喜欢的东西:

<LinearGradientBrush x:Key="TextBoxBorder" 
        StartPoint="0,0" 
        EndPoint="0,20" 
        MappingMode="Absolute"> 
    <LinearGradientBrush.GradientStops> 
     <GradientStop Color="#ABADB3" 
         Offset="0.05"/> 
     <GradientStop Color="#E2E3EA" 
         Offset="0.07"/> 
     <GradientStop Color="#E3E9EF" 
         Offset="1"/> 
    </LinearGradientBrush.GradientStops> 
</LinearGradientBrush> 

<Style x:Key="CustomTextBoxStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="Foreground" 
      Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Background" 
      Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="BorderBrush" 
      Value="{StaticResource TextBoxBorder}"/> 
    <Setter Property="BorderThickness" 
      Value="1"/> 
    <Setter Property="Padding" 
      Value="1"/> 
    <Setter Property="AllowDrop" 
      Value="true"/> 
    <Setter Property="FocusVisualStyle" 
      Value="{x:Null}"/> 
    <Setter Property="ScrollViewer.PanningMode" 
      Value="VerticalFirst"/> 
    <Setter Property="Stylus.IsFlicksEnabled" 
      Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBoxBase}"> 
       <DockPanel> 
        <Polygon x:Name="polygon" DockPanel.Dock="Left" Width="29" Points="14,8 14,21 28,14.5" Fill="Gray" Stroke="DarkGray"/> 
        <theme:ListBoxChrome x:Name="Bd" 
              BorderThickness="{TemplateBinding BorderThickness}" 
              BorderBrush="{TemplateBinding BorderBrush}" 
              Background="{TemplateBinding Background}" 
              RenderMouseOver="{TemplateBinding IsMouseOver}" 
              RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" 
              SnapsToDevicePixels="true"> 
         <ScrollViewer x:Name="PART_ContentHost" 
             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </theme:ListBoxChrome> 
       </DockPanel> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" 
          Value="false"> 
         <Setter TargetName="Bd" 
           Property="Background" 
           Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         <Setter Property="Foreground" 
           Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
        <Trigger Property="IsKeyboardFocusWithin" 
          Value="false"> 
         <Setter TargetName="polygon" 
           Property="Visibility" 
           Value="Collapsed"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

凡的xmlns theme被定义为xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"。您需要将参考添加到PresentationFramework.Aero.dll,或将其从ListBoxChrome更改为边框。

+0

老兄,你主宰了这个问题的地狱。我明天会看看这个。万分感谢。 – Yatrix

相关问题