开发者

XAML: Can I get "the next control" in a stacked panel and bind to it's property?

What I'm looking to do is make a polygon visible if the textbox in the stackedpanel next to it has f开发者_运维问答ocus. Basically, it'll be an indicator to which textbox has focus. This is going to be applied to multiple textboxes so I'd like to make it generic, using a style.

<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>


You would be better off building a custom TextBox style that includes the Polygon. You can get the default Styles from here.

Just take the default Style for TextBox and related resources, then add your polygon to the left.

Something like:

<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>

Where the xmlns theme is defined as xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero". You'd need to add a ref to PresentationFramework.Aero.dll, or change it from a ListBoxChrome to a Border.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜