-1

I have made a C# metro wpf application using mahapps with Visual Studio 2012. I have added a listbox to the window.

enter image description here

But when I re-size the window it stays in the same size.

enter image description here

This is my current code.

<controls:MetroWindow x:Class="My_app.MainWindow"
    WindowStartupLocation="CenterScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="My application" Height="500" Width="1000" EnableDWMDropShadow="True" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Closing="MetroWindow_Closing_1">


<Grid Margin="-1,0,1,0">
    <TextBox controls:TextboxHelper.Watermark="Search here..." controls:TextboxHelper.ClearTextButton="True" x:Name="textBoxSearch" HorizontalAlignment="Left" Height="23" Margin="11,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="255" TextChanged="textBoxSearch_TextChanged" PreviewKeyDown="textBoxSearch_PreviewKeyDown" BorderThickness="0.5"/>
    <Label x:Name="label1" Content="" HorizontalAlignment="Left" Margin="10,47,0,0" VerticalAlignment="Top" Height="17" Width="29"/>
    <ListBox x:Name="listBoxSuggestions" HorizontalAlignment="Left" Height="397" Margin="10,62,0,0" VerticalAlignment="Top" Width="255" SelectionChanged="listBoxSuggestions_SelectionChanged" PreviewKeyDown="listBoxSuggestions_PreviewKeyDown" GotFocus="listBoxSuggestions_GotFocus"/>
    <TextBox x:Name="textBoxResult" HorizontalAlignment="Left" Height="449" Margin="321,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="661" PreviewKeyDown="textBoxResult_PreviewKeyDown" HorizontalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" TextChanged="textBoxResult_TextChanged" BorderThickness="0"/>
</Grid>
</controls:MetroWindow>

The listbox's bottom should be the bottom of the window(Always keeping a small same gap). How can I achieve this ? How should I change my code ?

user3693167
  • 783
  • 1
  • 9
  • 16
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.* – Federico Berasategui Jul 28 '14 at 15:04
  • will add the code. Thought these details are enough for someone knows the issue – user3693167 Jul 28 '14 at 15:37
  • Yes. But it is unlikely to help future readers. Delete the fixed sizes and margins in your XAML and the horizontal / vertical alignments. Also, don't use the Visual Studio designer. – Federico Berasategui Jul 28 '14 at 15:41
  • Added the code. I used VS designer. The fixed sizes are there. Hope it will help to get the idea :) Thank you – user3693167 Jul 28 '14 at 15:44

1 Answers1

2

here you go

remove VerticalAlignment and Height from list box and it will resize the height according to window's height

<ListBox x:Name="listBoxSuggestions" HorizontalAlignment="Left" Margin="10,62,0,0"  Width="255" SelectionChanged="listBoxSuggestions_SelectionChanged" PreviewKeyDown="listBoxSuggestions_PreviewKeyDown" GotFocus="listBoxSuggestions_GotFocus"/>

if you need to resize the listbox's width according to the window's width also then remove HorizontalAlignment and Width too.

pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • Thats perfect. One more quesion please, how can I `center` the `title` of the window. It is coming `left aligned`. – user3693167 Jul 28 '14 at 16:49
  • 1
    try adding `TextBlock.TextAlignment="Center"` in `Window` element, if this does not work for you then perhaps you may define a custom WindowChrome for your window. see [How to create custom window chrome in wpf?](http://stackoverflow.com/questions/6792275/how-to-create-custom-window-chrome-in-wpf) for reference – pushpraj Jul 28 '14 at 23:59
  • Worked. It centered all the text inside other elements too. So I explicitly aligned them to Left. Thank you. – user3693167 Jul 29 '14 at 05:13
  • 1
    Great! btw if you have applied Left alignment to all other text blocks within you can apply TextBlock.TextAlignment="Left" to the root element ie. the first child of Window. if you have already done this way then cheers! happy coding :) – pushpraj Jul 29 '14 at 06:09