如何使用 ControlTemplate 中的应用程序资源?
我收到这个错误
在“System.Windows.Controls.ControlTemplate”的名称范围中找不到“disable_glow”名称。
尝试执行此操作时:
应用程序的资源:
<LinearGradientBrush Opacity="0.0" StartPoint="0,0"
EndPoint="0,1" x:Key="disable_glow" x:Name="disable_glow">
<GradientStop Offset="0.0" Color="#4D4D4D" />
<GradientStop Offset="0.1" Color="#404040" />
<GradientStop Offset="1.0" Color="#2E2E2E" />
</LinearGradientBrush>
在此处:
同一位置,在控件的 ControlTemplate 中样式:
<Border CornerRadius="4">
<Border.Background>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{StaticResource disable_glow}">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
...
当我使用 StaticResource 或 DynamicResource 关键字时,我收到相同的错误。
那么如何正确使用呢?
I get this error
'disable_glow' name cannot be found in the name scope of 'System.Windows.Controls.ControlTemplate'.
when trying to do this:
Application's resources:
<LinearGradientBrush Opacity="0.0" StartPoint="0,0"
EndPoint="0,1" x:Key="disable_glow" x:Name="disable_glow">
<GradientStop Offset="0.0" Color="#4D4D4D" />
<GradientStop Offset="0.1" Color="#404040" />
<GradientStop Offset="1.0" Color="#2E2E2E" />
</LinearGradientBrush>
in here:
Same place, in the ControlTemplate of the control Style:
<Border CornerRadius="4">
<Border.Background>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{StaticResource disable_glow}">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
...
When I use either StaticResource or DynamicResource keyword I get the same error.
So how to use it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 LinearGradientBrush 位于 ResourceDictionary(即 Resources 集合)中,则无需在 LinearGradientBrush 上设置
x:Name
属性。您只需设置x:Key
即可访问它。您收到的错误不会由
Brush="{StaticResourcedisable_glow}"
代码产生。如果未找到资源,则会显示“未找到资源”之类的信息。听起来您正在/正在尝试通过名称访问它。您需要确保 LinearGradientBrush 在 ControlTemplate 之前定义。
You don't need to set the
x:Name
attribute on your LinearGradientBrush, if it's in a ResourceDictionary (i.e. a Resources collection). You just need to setx:Key
to be able to access it.The error you are getting would not be produced by the
Brush="{StaticResource disable_glow}"
code. If the resources wasn't found it would say something like "Resource not found". It sounds like you are/were trying to access it by name.You would need to ensure that your LinearGradientBrush is defined before your ControlTemplate.