DockPanel LastChildFill 調整為 MinWidth? (DockPanel LastChildFill resizing to MinWidth?)


問題描述

DockPanel LastChildFill 調整為 MinWidth? (DockPanel LastChildFill resizing to MinWidth?)

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?

‑‑‑‑‑

參考解法

方法 1:

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.

方法 2:

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>

方法 3:

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. 

方法 4:

<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.

(by ZsoltLPLyo chauhanJSJyo chauhan)

參考文件

  1. DockPanel LastChildFill resizing to MinWidth? (CC BY‑SA 3.0/4.0)

#dockpanel #css #wpf






相關問題

為什麼 DockPanel.Dock="Bottom" 將元素放在頂部? (Why does DockPanel.Dock="Bottom" put element at the top?)

在 DevExpress 停靠面板中調整內容大小 (Resizing contents inside DevExpress Docking Panels)

DockPanel LastChildFill 調整為 MinWidth? (DockPanel LastChildFill resizing to MinWidth?)

如何更改 DockWindow 上 TitleBar 的背景色? (How to change the Backcolor of TitleBar on DockWindow?)

WPF:HorizontalAlignment=Stretch、MaxWidth 和 Right 同時對齊? (WPF: HorizontalAlignment=Stretch, MaxWidth, and Right aligned at the same time?)

如何使 DockPanel 中的項目擴展以適應 WPF 中的所有可用空間? (How to make items in a DockPanel expand to fit all available space in WPF?)

AvalonDock 的狀態和狀態,AvalonDock 的選項 (State and status of AvalonDock, options to AvalonDock)

如何讓唯一的 DockPanel 子元素停靠在頂部 (How to get sole DockPanel child element to dock on the top)

如果從 Microsoft 的代碼中拋出異常,我如何找出最初導致異常的原因? (How do I find out what originally caused an exception, if it gets thrown from Microsoft's code?)

WPF 拖放面板(如 iGoogle、BBC 網站、blacklight) (WPF Drag Drop Panel (like iGoogle, BBC website, blacklight))

如何使 WPF 文本框填充兩個按鈕之間的所有可用空間? (How to make a WPF TextBox fill all available space between two buttons ?)

DockPanel 不可見,即使它設置為可見 (DockPanel is not visible even though it is set to visible)







留言討論