Xavalon / XamlStyler

Visual Studio extension to help format your XAML source code
Apache License 2.0
1.21k stars 130 forks source link

Convert Text Content to Attribute #368

Open Montago opened 2 years ago

Montago commented 2 years ago

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" />
grochocki commented 10 months ago

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>
Montago commented 10 months ago

in the case you mention i would leave the elements.

its only text content that should be inserted into attribute

mrlacey commented 9 months ago

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.

Montago commented 9 months ago

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 ?)

mrlacey commented 9 months ago

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.