开发者

How to remove the button inside a Dockpanel from layout - WPF

I have a dockpanel with two buttons inside

    <Window x:Class="PracticeWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="True">
            <Button Name="btn1" DockPanel.Dock="T开发者_如何转开发op">Button 1</Button>        
            <Button Name="btn2" DockPanel.Dock="Top" Visibility="Collapsed">Button 2</Button>       
    </DockPanel></Window>

The problem is that the button btn2 is taking part in layout even if its Visibility="Collapsed".I expect that the btn1 must fill the whole space. Instead btn1 is sticking to the top position of the window. The above code must be equivalent to

<DockPanel LastChildFill="True">
        <Button Name="btn1" DockPanel.Dock="Top">Button 1</Button>
        <!--
        <Button Name="btn2" DockPanel.Dock="Top" Visibility="Collapsed">Button 1</Button>
        -->
    </DockPanel>

Why this is happening.


Vinod, in DockPanel, last item will always take the rest of the layout in DockPanel. as you have already set Top to your 1st button, Dock property on the second one will be ignored i think.

instead of using DockPanel, you might give "Grid" panel a try with AutoSize property ?


You can get the desired behavior by manually adding/removing button to/from DockPanel.Children property. More on MSDN Panel.Children article.


The above code must be equivalent to ...

No, that is just a false assumption on your part. The collapsed button is still the last child, so the first button is still docked to the top, the collapsed button has nothing to with that in terms of layout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜