开发者

WPF TextBox: error setting style

I have defined style for my textboxes that would show hints about what to enter when the textbox is empty.

This would be my window:

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type TextBox}" x:Key="stlHintbox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
                    <Setter Property="Background">
                        <Setter.Value>
                            <VisualBrush Stretch="None">
                                <VisualBrush.Visual>
                                    <TextBlock Text="First name" FontStyle="Italic" Foreground="LightGray" />
                                </V开发者_高级运维isualBrush.Visual>
                            </VisualBrush>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <TextBox Style="stlHintbox" />
</Grid>

And the error thrown is:

'Set property 'System.Windows.FrameworkElement.Style' threw an exception.' Line number '22' and line position '11'.

with inner exception of:

"'stlHintbox' is not a valid value for property 'Style'."

The style is working OK when placed inside <TextBox.Style>, so what am I doing wrong here?


Style="{StaticResource stlHintbox}"

You need to use the markup extension syntax to reference a resource from the dictionary.

If the resource is in-scope and available at design-time, you can use StaticResource, as in this case. If it will be available when all the pieces come together at runtime, such as from a resource dictionary or containing window/control, then you need DynamicResource, the downside being a total lack of compiler-checking.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜