XamlStyler icon indicating copy to clipboard operation
XamlStyler copied to clipboard

Convert Text Content to Attribute

Open Montago opened this issue 3 years ago • 5 comments
trafficstars

It would be nice to have the option to have XAML Styler to convert usual Text content into Attributes, especially for TextBlock, TextBox and Label :

<Label Grid.Row="0" Grid.Column="0">Empire</Label>

into

<Label Grid.Row="0" Grid.Column="0" Content="Empire" />

And

<TextBlock Grid.Row="0" Grid.Column="0">Empire</TextBlock>

into

<TextBlock Grid.Row="0" Grid.Column="0" Text="Empire" />

Montago avatar Feb 04 '22 10:02 Montago

The challenge with a feature like this is that there can be any combination of inline elements in the content. For example, how would you convert the following to an attribute?

<TextBlock>
    <Run FontSize="24" Foreground="#FFFC5185">Title Text</Run><LineBreak/>
    <Run>Lorem ipsum</Run>
</TextBlock>

grochocki avatar Dec 28 '23 21:12 grochocki

in the case you mention i would leave the elements.

its only text content that should be inserted into attribute

Montago avatar Dec 30 '23 11:12 Montago

Is there really enough value to do this for only TextBlock and Label?

I'm not sure how this could be implemented without shipping with a list of known elements and the attribute/property names that can be used when refactoring.

For example, <Button>click me</Button> would "collapse" to <Button Content="click me" />.

There is also a need to consider whether whitespace should be preserved when replacing an element with content that technically only contains text but also has whitespace (including newlines) around the text.

mrlacey avatar Jan 28 '24 18:01 mrlacey

this feature request is largely a "nice to have" to remove all the manual work in organizing the XAML (which is the point of the extension)

I would say that Content.Trim() would be the correct value to insert in the attribute field.

And yes : buttons would also make sense in this case., Maybe have a list of elementnames where this rule applies ?

Additionally, a threshold of when to convert could be made, eg: dont convert content when content length is above N letters (default 100 ?)

Montago avatar Jan 29 '24 13:01 Montago

Ha. Just seen that Blend does this to XAML that you drag in the designer 🤣

blend-button-content

I'm still, not advocating for it though.

mrlacey avatar Feb 01 '24 21:02 mrlacey