TextBox 的边框样式不会显示在 WPF 中的可编辑组合框中
我使用样式在文本框周围制作了一个漂亮的边框。请注意下面的边框,我将其简单命名为“Border”。当文本框接收 IsFocused-Trigger 中指定的焦点时,它工作正常:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ErrorTemplate}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Padding" Value="3,3,3,3" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border
Name="Border"
CornerRadius="4"
Padding="0"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1,1,1.4,1.4" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Border" Property="BorderThickness" Value="1.4,1.4,1.8,1.8"></Setter>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
</Trigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在我想在组合框中应用相同的边框样式。我读到可编辑组合框会带来一些麻烦,并且我发现建议使用 IsKeyboardFocusWithin 而不是 IsFocused。当我这样做时,我可以触发投影效果。那行得通。
但我的小边界不愿意。
我已将与上面相同的边框定义放在处理 ComboBox 类型的 ControlTemplate 下方的 Grid 部分中,然后希望可以使边框出现在组合框周围。目标名称显然已被识别,但仍然没有显示......
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter TargetName="Border" Property="BorderThickness" Value="1.4,1.4,1.8,1.8"></Setter>
</Trigger>
代码有什么问题?并且:是否可以将边框的定义放入自己的样式中,以便可以轻松引用文本框或组合框(因为引用正确的目标名称似乎存在问题)?
任何帮助将不胜感激!
I've made a nice border around a textbox using a style. Notice the border, which I've simply named "Border", below . It works fine when the textbox receives the focus as specified in the IsFocused-Trigger:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ErrorTemplate}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Padding" Value="3,3,3,3" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border
Name="Border"
CornerRadius="4"
Padding="0"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1,1,1.4,1.4" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Border" Property="BorderThickness" Value="1.4,1.4,1.8,1.8"></Setter>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
</Trigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now I wanted to apply the same border style at a combobox. I've read that editable comboboxes make a bit of trouble, and I saw that it is recommended to use IsKeyboardFocusWithin instead of IsFocused. When I do that, I can e.g.trigger a dropshadow effect. That works.
But my little border doesn't want to.
I've put the same Border definition as above in the Grid part below the ControlTemplate dealing with the type ComboBox and then hoped that I could make the border appear around the combobox. The targetname is obviously recognized, but still it doesn't show up...
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter TargetName="Border" Property="BorderThickness" Value="1.4,1.4,1.8,1.8"></Setter>
</Trigger>
What's wrong with the code? And: Can the definition of the border be put into an own style that can be referenced easily be it textbox or combobox (as there seems to be an issue with referencing the correct targetname)?
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
复制/粘贴/运行此:(我知道样式被夸大且不完整,但目的是表明您可以相对简单地做到这一点......并为您提供想法,并指出当缺少所需的属性时,您也可以随时触发该事件)
我在那里放置了一个带有一些文本块的堆栈面板,这样当您运行它时,您可以单击不同的它们与组合框来查看正在工作的触发器。
可能很明显,但并非如此,如果您想添加边框厚度的触发器,请确保将组合边距增加相同的量。
Copy/Paste/Run this: (I know the style's are exaggerated and incomplete, but the purpose is to show that you can do it, relatively simply...and to give you ideas, and point out that when the needed properties are missing, you can always trigger on the event too)
I put a stack panel with a few textBlocks there just so when you run it you can click different them vs. the combobox to see the triggers at work.
Might be obvious, but not that if you want to add triggers for Border thickness, make sure you increase the Combo Margin by the same amount.