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

发布于 2024-12-02 21:05:30 字数 656 浏览 1 评论 0原文

我想要做的是,如果旁边的 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>

What I'm looking to do is make a polygon visible if the textbox in the stackedpanel next to it has focus. 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找回味觉 2024-12-09 21:05:30

您最好构建一个包含多边形的自定义文本框样式。您可以从此处。

只需采用 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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文