This might be a stupid question, but i’m stuck doing it :(. I have a grid and have 3 columns. I have a textbox and a listbox in each of these 3 columns as shown:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130"></ColumnDefinition>
<ColumnDefinition Width="380"></ColumnDefinition>
<ColumnDefinition Width="146"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0">
<TextBox Text="File Name" Height="30"></TextBox>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="0">
<TextBox Text="File Path" Height="30"></TextBox>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="0">
<TextBox Text="File Size" Height="30"></TextBox>
</StackPanel>
<StackPanel Grid.Column="0">
<ListBox Name="listbox_name" Margin="1,30" Height="276" />
</StackPanel>
<StackPanel Grid.Column="1">
<ListBox Name="listbox_path" Margin="1,30" Height="276" />
</StackPanel>
<StackPanel Grid.Column="2">
<ListBox Name="listbox_size" Margin="1,30" Height="276" />
</StackPanel>
and the code behind it:
public Window1()
{
InitializeComponent();
list.Add("D:\\a\\hy");
list.Add("D:\\a\\hy1");
list.Sort();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listbox_name.ItemsSource = list;
grid1.Visibility = Visibility.Hidden;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
grid1.Visibility = Visibility.Visible;
}
But on the click of the button, im not able to see the listboxes, with the list displayed. Please guide me as to where im going wrong. Thanks!
The reason is that your
stackPanelis inGrid.Col="0"and it is very small. ButListBoxis inside of yourstackPanel. It has a margin and it goes down. Is you can’t see yourlistBox.If you will do something like this:
you will see your listBox and it will work.
NOTE: this code is only example. You need to make a better layout for your window.
Here is how i made a window loyout: