开发者

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?


Something like this would be more appropriate:

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

Here is an overview of what can go into a TextBlock's content, more specifically here.


one more way is

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

btw Damascus's solution adds more UI Elements. with CodeNaked's solution, its difficult to databind the Text.


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)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜