Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 696
oscilatingcretin Avatar asked Oct 17 '25 07:10

oscilatingcretin


2 Answers

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.

like image 68
CodeNaked Avatar answered Oct 20 '25 00:10

CodeNaked


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.

like image 42
krishnaaditya Avatar answered Oct 20 '25 01:10

krishnaaditya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!