0

I have the problem that the content of the expander does not start at the left, how can I solve this? As you can see on the picture, the contents of my expander are not shown to the far left. I have tried many settings, unfortunately I do not come to the solution. The expander itself also seems to have an edge opposite the stackpanel, which I also want to avoid. Above the expander you can see a grid, which contains a text block with content. This grid has the right mass within the higher-level stack panel.

Expander exp = new Expander
        {
            Header = "TestExpander",
            Width = spStackPanel.Width,
            ExpandDirection = ExpandDirection.Down,
            IsExpanded = true,
            BorderBrush = Brushes.Yellow,
            BorderThickness = new Thickness(1),
            HorizontalAlignment = HorizontalAlignment.Left,
            HorizontalContentAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
            VerticalContentAlignment = VerticalAlignment.Top,
        };

        TextBox tx = new TextBox
        {
            Text = "testText"
        };

        exp.Content = tx;

        spStackPanel.Children.Add(exp);

enter image description here

user2250152
  • 14,658
  • 4
  • 33
  • 57
Sidlercom
  • 11
  • 4
  • 1
    You can observe visual tree using snoop tool or a [build-in](http://codemindinterface.com/2015/03/xaml-debugging-tools-vs-snoop/) VS one. This should help you to find exact reason why something is not aligned as you think it should be. – Sinatr Apr 22 '21 at 08:50
  • This is not a helpful tip, I try to find the error already with the help of vs – Sidlercom Apr 22 '21 at 09:06
  • Please share your xaml with spStackPanel. The issue is not reproducible. – user2250152 Apr 22 '21 at 09:40
  • There is no XML file, I do this directly in .cs – Sidlercom Apr 22 '21 at 13:05

2 Answers2

0

I can't comment so apologies for the somewhat vague answer, but it might help to share the styling and XAML used to create your screenshot. By default that isn't how an Expander looks, so my guess is that something in your styling or one of the higher level controls (e.g. StackPanel or Grid) is affecting the layout of the Expander.

Example being possibly a keyless style for TextBox that applies padding and/or horizontal alignment, which would then affect the content you're adding by creating a new TextBox in your snippet to give it that odd alignment.

Aelarion
  • 397
  • 4
  • 11
0

I have written a test here, directly in a .cs file WITHOUT XML:

buttonGrid = new Grid();
        buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
        buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });

        Expander exp = new Expander()
        {
            Header = "Test Expander",
            HorizontalAlignment = HorizontalAlignment.Left,
            Margin = new Thickness(0),
            IsExpanded = true
        };

        TextBlock label = new TextBlock()
        {
            HorizontalAlignment = HorizontalAlignment.Left,
            Margin = new Thickness(0),
            Text = "Test Text"
        };
        exp.Content = label;
        Grid.SetRow(exp, 0);
        buttonGrid.Children.Add(exp);

        TextBlock text = new TextBlock()
        {
            HorizontalAlignment = HorizontalAlignment.Left,
            Margin = new Thickness(0),
            Text = "Test Text",
            Foreground = Brushes.White
        };
        Grid.SetRow(text, 1);
        buttonGrid.Children.Add(text);

I know that an expander doesn't usually look like this, so I ask my question ;-) maybe someone has me a tip to look for.

enter image description here

Sidlercom
  • 11
  • 4