为什么我不能将 Orientation 属性添加到 WPF 中的样式设置器中
当我写下这样的内容时:
<Style x:Key="panelS">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="DockPanel.Dock" Value="Top" />
</Style>
我收到错误消息:无法解析样式属性“方向”。验证所属类型是否为 Style 的 TargetType,或使用 Class.Property 语法指定 Property。
当然,我有一个 Dock 面板,其中有许多 Stackpanels,所以我想将 Stackpanel 的属性移至样式。但是存在这个错误,我不太明白它的含义和解决方法是什么(..我不想在每个堆栈面板上分配方向)。
When i write something like this:
<Style x:Key="panelS">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="DockPanel.Dock" Value="Top" />
</Style>
I get the error that says: Cannot resolve the Style Property 'Orientation'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.
Sure i have a Dock panel with many Stackpanels in it so i want to move Stackpanel's properties to the style. But there is this error and i dont quite understand what it means and what is the workaround(..i'd wanted not to assign Orientation on every Stackpanel).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
Style
与StackPanel
类型无关。因此,WPF 不知道
Orientation
属性。 (因为该属性是由 StackPanel 定义的)您可以通过将其更改为 StackPanel.Orientation 来明确告诉 WPF 哪个类定义了该属性。
或者,您可以通过将
TargetType="StackPanel"
添加到Your
Style
isn't associated with theStackPanel
type.Therefore, WPF doesn't know about the
Orientation
property. (Because that property is defined byStackPanel
)You can explicitly tell WPF which class defines the property by changing it to
StackPanel.Orientation
.Alternatively, you can associate the
Style
with theStackPanel
type by adding aTargetType="StackPanel"
to the<Style>
element.尝试将 TargetType 添加到样式中,以便它知道您正在谈论 StackPanels。这个版本应该可以工作:
Try adding a TargetType to the style, so that it knows you are talking about StackPanels. This version should work: