开发者

How to use application's resource from ControlTemplate?

开发者 https://www.devze.com 2023-03-07 17:30 出处:网络
I get this error \'disable_glow\' name cannot be found in the name scope of \'System.Windows.Controls.ControlTemplate\'.

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>
                            <RectangleGe开发者_Go百科ometry 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?


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 set x: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.

0

精彩评论

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