UiTextBox and ComboBox ignore Width when HorizontalAlignment is set
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 9.6k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
For at least two controls I've noticed, if you set the Width property (in XAML), and also set the HorizontalAlignment property, Width will be ignored.
To Reproduce
- Create a simple XAML page with a Grid.
- Set the Width of the Page or Grid to have excess space (stretch to take up the whole page)
- Add a UITextBox or ComboBox control, and set the Width as well as HorizontalAlignment:
<ui:TextBox Grid.Column="1" Grid.Row="1" Text="SomeText" Width="250" HorizontalAlignment="Left"/>
Run the app and display the page
Expected behavior
Expected: The UiTextBox or ComboBox will be the requested Width and be aligned to the left side of the containing grid.
Actual: The control is aligned to the Left but is much smaller than the set Width.
Note: You can see the problem in design mode as well, in Visual Studio 2022. Remove the HorizontalAlignment property setter and then Width is correct.
Screenshots
No response
OS version
Windows 11
.NET version
.Net 9.0
WPF-UI NuGet version
3.0.5
Additional context
The issue can be resolved by modifying the source's original Style. In the case of a TextBox, the DefaultTextBoxStyle defined in TextBox.xaml:
... <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> <!--- TO FIX: remove the HorizontalAlignment and VerticalAlignment setters: <Grid> --> <Border x:Name="ContentBorder" MinWidth="{TemplateBinding MinWidth}" ...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the layout in a WPF XAML page, then inspect the DefaultTextBoxStyle in TextBox.xaml and the corresponding ComboBox style. Verify that removing the template Grid's alignment bindings makes the requested Width work with HorizontalAlignment, and confirm the behavior for both controls in the sample layout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100