C# 以编程方式将WPF DockPanel中的最后一个子级替换为LastChildFill true
当DockPanel的LastChildFill属性设置为true时,添加的最后一个子项将占用整个未使用的空间 这很好,直到我不得不以编程方式替换最后一个孩子:C# 以编程方式将WPF DockPanel中的最后一个子级替换为LastChildFill true,c#,wpf,dockpanel,C#,Wpf,Dockpanel,当DockPanel的LastChildFill属性设置为true时,添加的最后一个子项将占用整个未使用的空间 这很好,直到我不得不以编程方式替换最后一个孩子: UIElementCollection children = myDockPanel.Children; UIElement uie = new myBestControl(); children.RemoveAt(children.Count - 1); children.Add(uie); 现在,新添加的控件不再填充空间 我应该如
UIElementCollection children = myDockPanel.Children;
UIElement uie = new myBestControl();
children.RemoveAt(children.Count - 1);
children.Add(uie);
现在,新添加的控件不再填充空间
我应该如何解决这个问题?使用这个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>
当您单击text one时,文本块将被替换为一个设置了背景的按钮,因此您可以看到它发生了。谢谢Simeon。原来我的困惑源于我对填充的误解。显然,填充并不一定意味着占据整个空间。相反,如果指定了新添加的控件的大小,则该控件将居中
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);
}