question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using setter for TextBlock.Text property breaks some other TextBlocks

See original GitHub issue

Describe the bug When I define a Style for TextBlock with Setter for Text property, it removes (not even replaces) text on some other TextBlock controls.

To Reproduce Minimal XAML example I’ve came up with:

<Window xmlns="https://github.com/avaloniaui"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:vm="using:TextBlockTextSetterBug.ViewModels"
		xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
		mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
		x:Class="TextBlockTextSetterBug.Views.MainWindow"
		Icon="/Assets/avalonia-logo.ico"
		Title="TextBlockTextSetterBug">
	<Window.Styles>
		<Style Selector="TextBlock.myautotext">
			<Setter Property="Text" Value="Text from Style" />
		</Style>
	</Window.Styles>

	<Design.Height>200</Design.Height>
	<Design.Width>300</Design.Width>
	<Design.DataContext>
		<vm:MainWindowViewModel/>
	</Design.DataContext>

	<StackPanel>
		<!-- This text IS visible (style works as expected) -->
		<TextBlock Classes="myautotext" />
		
		<!-- This text IS visible (bindings outside DataTemplate are working) -->
		<TextBlock Text="{Binding Greeting}" />
	
		<!-- This text is NOT visible (direct setting of text doesn't work) -->
		<TextBlock Text="Can't see me" />
	
		<!-- Text of items is NOT visible (using explicit DataTemplate changes nothing) -->
		<ListBox Items="{Binding SomeStrings}"/>
	</StackPanel>
</Window>

Expected behavior TextBlock controls not matching the selector must not be affected.

Screenshots Expected: expected

Actual: actual

Desktop

  • OS: Windows 10
  • Avalonia 0.10.13 (latest from NuGet)

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:19 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
grokyscommented, Apr 15, 2022

The reason is kind of lost in the mists of time, but I think it was done for performance reasons. Reading a CLR (i.e. direct) property is about 40x faster currently than reading a styled property. That combined with:

  • It will almost always be set
  • The need to style it is uncommon
  • TextBlocks are very common and there will likely be a lot of them
  • In CSS you can’t style element contents

I think lead us to decide that it was worth sacrificing some flexibility for performance.

1reaction
robloocommented, Apr 15, 2022

@grokys Hm, in my opinion this feature shouldn’t be sacrificed. It is something I would expect to just work and does in all other XAML frameworks. That said, I don’t know the performance issues encountered at the time, or if they are still valid. I think this should also be documented some place along with why it was designed this way.

If the styling system is really this slow, and this is still a problem enough to keep Text a direct property, I guess there are more fundamental concerns that should be addressed. WPF can handle this type of thing just fine and it’s performance was well enough – obviously it never ran on all the form factors Avalonia does though.

Overall, this is pretty unintuitive and has/will cause a lot of tricky debugging. I definitely think compile warnings are needed but I think this should just be a StyledProperty for TextBlock (leave TextBox as-is). Optimizations have since been made to the styling system over the years as well.

Finally, are we sure there isn’t more going on?:

		<!-- This text is NOT visible (direct setting of text doesn't work) -->
		<TextBlock Text="Can't see me" />

There is no way this should ever not work. I guess it is related to the other issue of priority that might be fixed after your PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overwrite Text property in a Setter (or a better approach)
I'm struggling with replicating a switch in WPF XAML, where I want to go through some bindings hierarchically and set my Text property...
Read more >
Control When the TextBox Text Updates the Source
This topic describes how to use the UpdateSourceTrigger property to control the timing of binding source updates. The topic uses the TextBox ......
Read more >
Untitled
Using setter for TextBlock.Text property breaks some other WebDec 5, 2011 · hi For my TextBlock Text, i've declared a MultiBinding with StringFormat...
Read more >
Unable to Bind Text to TextBlock in TileGroupHeader
Hi, I am unable to bind the text to TextBlock which is there in TileGroupHeader of TileLayoutControl. It is working when we give...
Read more >
TextBox Mask XAML Property - Windows Community Toolkit
The TextBoxMask Property allows a user to more easily enter fixed width text in TextBox control where you would like them to enter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found