【问题标题】:Programmatically Replacing Last Child in a WPF DockPanel with LastChildFill true以编程方式将 WPF DockPanel 中的最后一个子项替换为 LastChildFill tr​​ue
【发布时间】:2009-12-07 01:15:39
【问题描述】:

DockPanelLastChildFill 属性设置为 true 时,最后添加的子项会占用整个未使用的空间。

效果很好,直到我不得不以编程方式替换最后一个孩子:

UIElementCollection children = myDockPanel.Children;
UIElement uie = new myBestControl();
children.RemoveAt(children.Count - 1);
children.Add(uie);

现在,新添加的控件不再填充空间。

我应该如何解决这个问题?

【问题讨论】:

    标签: c# wpf dockpanel


    【解决方案1】:

    这个 xaml 对我有用

    <DockPanel x:Name="MyDock" LastChildFill="True">
        <TextBlock DockPanel.Dock="Left" 
                   MouseDown="TextBlock_MouseDown">Child 1</TextBlock>
        <TextBlock DockPanel.Dock="Left">Child 2</TextBlock>
        <TextBlock>Child 3</TextBlock>
    </DockPanel>
    

    还有这段代码

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        UIElementCollection children = MyDock.Children;
        Button btn = new Button();
        btn.Background = Brushes.LightBlue;
    
        children.RemoveAt(children.Count - 1);
        children.Add(btn);
    }
    

    当您单击文本之一时,文本块被替换为一个设置了背景的按钮,因此您可以看到它已经发生了。

    【讨论】:

      【解决方案2】:

      谢谢西蒙。原来我的困惑来自我对“填充”的误解。显然,“填充”并不一定意味着占据整个空间。相反,如果指定了新添加的控件的大小,它将居中。

      【讨论】:

        猜你喜欢
        • 2012-07-23
        • 2011-01-05
        • 2015-09-17
        • 1970-01-01
        • 2013-11-10
        • 2013-09-17
        • 2018-11-30
        • 2010-10-21
        • 1970-01-01
        相关资源
        最近更新 更多