-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using setter for TextBlock.Text property breaks some other TextBlocks #7982
Comments
Text is a direct property and not styled property. It's not supposed to be used in styles. |
Thank you for the answer. Does it mean there's no (clean) way to apply the same text to a bunch of |
Think about properties as it can be either styleable or data.
At once - no. You can bind them to the same property though, as you can in WPF. |
Well, it's hard to distinguish between "data" and "style" sometimes. In my case, I have many Having to write the text is not an issue, and there's no reason to complicate things with bindings. Thank you again for your time. I'm keeping this issue open until the compile time error you mentioned comes in then. :-) |
TextBlock.Text absolutely should be styleable. @maxkatz6 This can be done in every-single other XAML framework, correct? Edit: Here is WPF source It definitely can be used like any other property. Unless I'm missing something so obvious I never thought about it before, I would expect the above XAML to work. You might be able to argue with a |
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:
I think lead us to decide that it was worth sacrificing some flexibility for performance. |
@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?:
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. |
Wrong. See, e.g.: foo:before {
content: "In a galaxy far, far away... ";
}
foo:after {
content: "That's all, folks!";
} See the definition of content CSS property for more details. While technically it doesn't allow to set actual content, it at least allows what I wanted to do initially. :) |
Since |
Technically TextBlock will be able to host everything that is of type |
@Gillibald should we make TextBlock.Text a styled property without inlines support and instead add RichTextBlock with direct property that also can work with inlines? |
Yes. I think that is the best solution. |
While I have no idea how you already support rich text and inlines in a property declared as type string, I actually don't think this should change. It should function like WPF where you absolutely can do inclines and complex text formatting directly as TextBlock content. |
This is purely for optimization. TextBlock has an InlinesProperty marked as Content and a TextProperty that can be used to add text to current text layout. Introducing a RichTextBlock allows us to have some performance hits. We could support text selection etc. A regular TextBlock is unformatted most of the time so it makes sense to optimize the primitive in that way. |
@Gillibald That does make some sense. However, I think you are now making the difference between Label and TextBlock even more of a grey area. I think the performance impact of supporting inlines in TextBlock is negligible as well. If the content value is a string (a very simple check) it can bypass all inlines processing. Bottom line, I'm not sure we need Label, TextBlock and now RichTextBlock. Plus we are breaking WPF compatibility which is somewhat bad in fundamental cases like this. However, RichTextBlock does conceptually match with a future RichTextBox as well. |
We will need RichTextBlock pretty much anyway. At least for text selection. WPF TextBlock doesn't have that. And in UWP only RichTextBlock has text selection, but not TextBlock. And about inlines. In the stable branch we don't have inlines, so it won't be a breaking change anyway. Not a huge difference for developers coming from WPF either. But it still will cause some confusion, you are right. Though, I don't really know much details about perf-related reasoning, so I won't argue on that too much. And if we can get styled property with inlines or not. |
Well, text selection could be added pretty easily (at least in API) to TextBlock with a new enable selection property. However, I see some appeal in separating the advanced formatting out like RichTextBox has historically. I just:
Anyway, I don't have very strong feelings about this so if you all think it's a good idea I won't put up any more resistance. I'll get used to the cleaner separation of concerns with plain text in TextBlock and formatted text in RichTextBlock sooner rather than later. |
@maxkatz6 Why was this closed? It seemed to be OK according to @Gillibald comments who only cautioned against changing TextBox.Text from direct to styled? |
Describe the bug
When I define a
Style
forTextBlock
withSetter
forText
property, it removes (not even replaces) text on some otherTextBlock
controls.To Reproduce
Minimal XAML example I've came up with:
Expected behavior
TextBlock
controls not matching the selector must not be affected.Screenshots
Expected:
Actual:
Desktop
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: