XAML:我可以在堆叠面板中获得“下一个控件”并绑定到它的属性吗?

时间:2011-08-31 20:01:41

标签: xaml binding styles

我想要做的是,如果其旁边的stackedpanel中的文本框具有焦点,则可以看到多边形。基本上,它将成为文本框关注的指标。这将应用于多个文本框,所以我想使用样式使其成为通用的。

<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 个答案:

答案 0 :(得分:1)

您最好构建包含Polygon的自定义TextBox样式。您可以从here获取默认样式。

只需采用TextBox和相关资源的默认样式,然后将多边形添加到左侧。

类似的东西:

<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更改为Border。