Question

I try to achieve a simple menubar in WPF.

Here is the XAML:

<Page
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <DockPanel>  
    <DockPanel Background="Black" VerticalAlignment="Top" LastChildFill="True" DockPanel.Dock="Top" Height="28">
        <ToggleButton Content="--" Visibility="Collapsed" />
        <StackPanel Orientation="Horizontal">
           <Button Content="Add" />
           <Button Content="Expand" /> 
        </StackPanel>
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
           <TextBox Text="Search" MinWidth="80" Width="200" />
           <Button Content="X" Margin="0,1,50,0" />
        </StackPanel>
    </DockPanel>
 </DockPanel>
</Page>

It looks good, but when I resize the page to a smaller width, the last child (the Stackpanel with the search textbox) is hiding behind the left items. Like this: http://s9.postimage.org/m0tkrobwd/printscreen.png

It would be good if the textbox would resize itself if it has enough space to achieve its MinWidth...Is it possible?

Was it helpful?

Solution

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                      <ColumnDefinition Width="*"/>
                    <ColumnDefinition MaxWidth="200" MinWidth="80"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ToggleButton Content="--" Visibility="Collapsed" />
                <Button Content="Add"  Grid.Column="1"/>
                <Button Content="Expand"  Grid.Column="2"/>
                <TextBox Text="Search" Grid.Column="4"/>
                <Button Content="X" Margin="0,1,50,0" Grid.Column="5" />
            </Grid>

Instead of giving MaxWidth and MinWidth to TextBox give it to Grid Column.I hope this will help.

OTHER TIPS

The tricky thing is to give a Control (in your case the SearchTextBox) an alignment but still catch the available space up to MaxWidth. Thanks to this answer.

<DockPanel>
    <DockPanel Background="Black" DockPanel.Dock="Top" Height="28">
        <ToggleButton DockPanel.Dock="Left" Content="--" Visibility="Collapsed" />
        <StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
            <Button Content="Add" />
            <Button Content="Expand" />
        </StackPanel>
        <Button DockPanel.Dock="Right" Content="X" />
        <Border Name="Container">
            <TextBox Text="Search" HorizontalAlignment="Right"
                     Width="{Binding ElementName=Container, Path=ActualWidth}" MaxWidth="200" />                                
        </Border>
     </DockPanel>        
</DockPanel>

Container could be another Control too.

Hi Set MaxWidth and MinWidth for the TextBox Not Width. If you will set the Width then it has highest precedence and hence MinWidth won'nt come into act.I hope this will help.

 <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ToggleButton Content="--" Visibility="Collapsed" />
                <Button Content="Add"  Grid.Column="1"/>
                <Button Content="Expand"  Grid.Column="2"/>
                <TextBox Text="Search" MinWidth="80" MaxWidth="600" Grid.Column="3" ClipToBounds="True" />
                <Button Content="X" Margin="0,1,50,0" Grid.Column="4"/>
            </Grid>

Its just because of the size of page is not enough for showing both stackpanel on page. for this you can use the minimum size of the page. that will prevent the damages of the control hiding .

and its also up to you what design you want. you can either use grid rather then stackpanles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top