tags:

views:

11

answers:

1

Hi,

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" />
                                </VisualBrush.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?

+2  A: 
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.

Jay
Duh... I'm just beginning wpf, I completely forgot about this... thanks
veljkoz