开发者

Research help: relative access to a Text Box's name with _'s replaced as " " wpf

I'm trying to label text boxes with the name of the field which is to be put into them, and currently I have ever开发者_如何学编程ything working in a static manner (I have to copy and paste the style into each new textbox and change the text to the appropriate field name). Is there a way to do this dynamically, so that I write a style that accesses the TextBox's name, replaces the "_"s with " "s and then puts that in my visualbrush's textblock? I'd appreciate research links or the names of relevant topics, I'm just not sure what to be looking for.

Here is my style:

<TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock FontSize="32" Foreground="Gray" >First Name</TextBlock>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>


If you find yourself copying and pasting lots of the same code, then you probably want to create a Custom Control. That would probably be the simplest solution (and, generally speaking, the simplest solution is the best solution).

Here is one of many tutorials I found by Googling WPF Custom Control.

Another possibility (which could be used in conjunction with or instead of the custom control) is the use of Attached Properties.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜