Search code examples
wpffieldrequired

WPF: Is there a sort of XAML tag that behaves like the HTML span element?


Coming from ASP.NET, this WPF stuff is just confusing. All I want to do is put a red asterisk by a label to indicate a required field. Playing around with stuff, I found that this actually does the trick:

<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Label Foreground="Red" Content="*" /><Label Content="Heavy Weight" />
</TextBlock>

Being that I just came up with this, I am not convinced it's the academic route a seasoned WPF developer would take. Additionally, this markup puts a huge amount of white space in between the asterisk and the label. In HTML, a span element would just render right beside its next sibling element. FYI, I tried putting a label within a label, but VS2010 kept barking about "The property 'content' is set more than once".

Any ideas?


Solution

  • The explanation is that you actually put two elements one after the other. You need to put them into a container.

    Just a sample code of a sentence with red asterisk I did recently:

    <StackPanel  Orientation="Horizontal" Margin="5" >
                <TextBlock Text="Display name"/>
                <TextBlock Text="*" Foreground="Red" FontWeight="Bold" />
                <TextBlock Text=":"/>
            </StackPanel>
    

    There, everything is in a StackPanel, so property 'content' will actually be set once (if you don't specify a group panel such as this one, you'll have to add only one element)