ContentPresenter Content does not shown Correctly in a ItemsRepeater

Mahdi Hosseini 155 Reputation points
2025-01-06T21:20:40.16+00:00

Hi, i am creating a StepBar Control, in Vertical Orientation, and Left or Right DisplayMode (for Header Text), but ContentPresenter Content does not shown correctly.

as you can see BasicInfo shown as BasicInf and UploadFile shown as UploadF.

Screenshot 2025-01-07 003425 and this a Left DisplayMode

22

i uploaded a repro/sample project.

StepbarSample2.zip

my StepBarItem is a

public partial class StepBarItem : ContentControl

and ControlTemplate defined like this:

<StackPanel VerticalAlignment="Top"
                                    FlowDirection="{TemplateBinding FlowDirection}"
                                    Orientation="Horizontal"
                                    Spacing="5">
                           
                            <Border x:Name="PART_Border"
                                    Height="40"
                                    MinWidth="40"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Background="{ThemeResource ControlSolidFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource StepBarItemInActiveBorderBrush}"
                                    BorderThickness="2"
                                    CornerRadius="90"
                                    FlowDirection="LeftToRight">
                                <TextBlock x:Name="PART_Number"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Foreground="{ThemeResource TextFillColorPrimaryBrush}"
                                           Style="{StaticResource BaseTextBlockStyle}"
                                           Text="{Binding Index, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                            </Border>
                            <ContentPresenter x:Name="PART_Content"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              Foreground="{ThemeResource TextFillColorPrimaryBrush}" />
                        </StackPanel>

and my StepBar is a

public partial class StepBar : Control

which i am using a ItemsRepeater with following Layout:

itemsRepeater.Layout = new UniformGridLayout()
        {
            MaximumRowsOrColumns = GetItemsCount(),
            ItemsStretch = Orientation == Orientation.Horizontal ? UniformGridLayoutItemsStretch.Fill : UniformGridLayoutItemsStretch.None,
            ItemsJustification = UniformGridLayoutItemsJustification.SpaceBetween,
            Orientation = this.Orientation
        };

and this is my StepBar ControlTemplate:


 <Grid Padding="{TemplateBinding Padding}"
                                          HorizontalAlignment="Left"
                                          VerticalAlignment="Top"
                                          Background="{TemplateBinding Background}"
                                          BorderBrush="{TemplateBinding BorderBrush}"
                                          BorderThickness="{TemplateBinding BorderThickness}"
                                          CornerRadius="{TemplateBinding CornerRadius}">
                                        <Grid.Resources>
                                            <x:Double x:Key="ProgressBarMinHeight">4</x:Double>
                                            <x:Double x:Key="ProgressBarTrackHeight">4</x:Double>
                                        </Grid.Resources>

                                        <ProgressBar x:Name="PART_ProgressBarBack"
                                                     Margin="0,20,0,0"
                                                     HorizontalAlignment="Center"
                                                     VerticalAlignment="Top" />
                                        <Rectangle x:Name="PART_Rectangle"
                                                   Width="4"
                                                   Margin="28,10,28,0"
                                                   HorizontalAlignment="Left"
                                                   VerticalAlignment="Top"
                                                   Fill="{ThemeResource ProgressBarBackground}"
                                                   Visibility="Collapsed" />
                                        <Rectangle x:Name="PART_RectangleFill"
                                                   Width="4"
                                                   Height="0"
                                                   Margin="28,10,28,0"
                                                   HorizontalAlignment="Left"
                                                   VerticalAlignment="Top"
                                                   Fill="{ThemeResource ProgressBarForeground}"
                                                   Visibility="Collapsed" />
                                        <ItemsRepeater x:Name="PART_ItemsRepeater"
                                                       ItemsSource="{TemplateBinding Items}" />
                                    </Grid>

why ContentPresenter does not shown correctly? it seems that we can show full content by set a Width="200" for ContentPresenter, but this is not a solution, because content can be anything and width can be anything i cant set a width 200 for all items because some of them may need less or greater width. i need a Auto Width. i dont know why it is not working?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,041 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
809 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,193 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.