Home > Back-end >  Can't see whole object in StackPanel?
Can't see whole object in StackPanel?

Time:06-19

I am a newbie in WPF trying to create his first normal project which is a notes application. Every note is linked to a button and I thought it'll look beautiful in StackPanel. I am dynamically creating buttons within StackPanel, but the problem is that when I scroll down the list of Buttons last Button can't be seen fully. I thought it was because of the Margin and tried to adjust it, but it didn't help, also auto Height and Width didn't help. StackPanel is within ScrollViewer and when I reach the end of ScrollBar I just can see half of last button ?

Here is StackPanel's XAML code:

    <ScrollViewer VerticalScrollBarVisibility="Auto" 
    Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="50 -50 50 5" >
                <StackPanel x:Name="ButtonPanel"/>
    </ScrollViewer>

That is how I create Button:

            dynamicTextBox.Add(SearchNotes); 
            dynamicTextBox[index_of_buttons] = new TextBox();
            Grid.SetRow(dynamicTextBox[index_of_buttons], 1);
            Grid.SetColumn(dynamicTextBox[index_of_buttons], 0);
            this.ButtonPanel.Children.Add(dynamicTextBox[index_of_buttons]);
            dynamicTextBox[index_of_buttons].IsReadOnly = false;
            dynamicTextBox[index_of_buttons].Text = "";

Where dynamicTextBox is List<> of TextBoxes to which is applied Template(I think it is not necessary to see Template to resolve the problem)

This is how it looks like: Can't see the whole button(the last one)

So I wanna see the whole button.

CodePudding user response:

As Andy wrote in the comment section, ListBox is best solution for this problem. I tried this one to make it look like exactly same as it was in StackPanel

Xaml:

<ListBox Name="ListBoxOfButtons" Background="#404040" BorderThickness="0" 
                 HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="0" 
                 Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="50 -20 50 5">
</ListBox>

C#:

var txtBox = new TextBox();
txtBox.Template = FindResource("TemplateForTextBox") as ControlTemplate;
ListBoxOfButtons.Items.Add(txtBox);

This is how last button looks like now

  • Related