【问题标题】:Rearrange stackpanels with a number of items in them - WPF and C#重新排列堆栈面板,其中包含许多项目 - WPF 和 C#
【发布时间】:2017-02-24 18:20:24
【问题描述】:
我目前有七个不同的面板,其中包含按钮、文本框和其他项目。每个面板在水平方向上都很长并且当前堆叠在彼此的顶部,并且所有面板都在滚动查看器内部并且开始时不可见。 Above the scroll viewer is a listbox with the names of each of the seven different panels and when one is selected, that panel is made visible.我试图找到一种方法,以便当从列表框中选择一个面板时,它将该面板移动到滚动查看器的顶部。我已经查看了项目控制示例,但他们没有说明在这种情况下如何控制组或面板中的多个项目。抱歉,如果这有点令人困惑,谢谢您的帮助。
【问题讨论】:
标签:
c#
wpf
panel
scrollviewer
stackpanel
【解决方案1】:
首先,您在ScrollViewer 中使用的是哪个主容器面板?正如您所说,堆叠了 7 个面板,所以我假设您在 ScrollViewer 内有一个 StackPanel。
现在,您需要操纵 Children collection 或 StackPanel。
例如;
<ScrollViewer Margin="0,61,0,0">
<StackPanel x:Name="MainPnl">
... 7 panels ...
</StackPanel>
</ScrollViewer
代码:
private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
var item = (sender as ComboBox).SelectedItem;
string name = (item as ComboBoxItem).Content.ToString();
Panel pnl = FindName(name) as Panel;
int indexof = MainPnl.Children.IndexOf(pnl);
MainPnl.Children.RemoveAt(indexof);
MainPnl.Children.Insert(0, pnl);
}