如何动态地将CheckBox放在StackPanel中?

时间:2016-12-05 16:55:13

标签: c# wpf xaml

我已经创建了一个XAML对话框窗口。在大多数情况下,我需要动态填充此窗口,这是我以前从未尝试解决的问题,而且我无法在此网站或MSDN上找到任何内容。这是我在.xaml

中展示的内容
<GroupBox Header="Pipe Segments" Height="Auto" Margin="5,0,0,0">
    <StackPanel Orientation="Vertical">
        <StackPanel x:Name="PipeSegRow1" Orientation="Horizontal" Margin="0,5,0,0">
            <StackPanel x:Name="PipeSegR1C1" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR1C2" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR1C3" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR1C4" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR1C5" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR1C6" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
        </StackPanel>
        <StackPanel x:Name="PipeSegRow2" Orientation="Horizontal" Margin="0,5,0,0">
            <StackPanel x:Name="PipeSegR2C1" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR2C2" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR2C3" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR2C4" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR2C5" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR2C6" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
        </StackPanel>
        <StackPanel x:Name="PipeSegRow3" Orientation="Horizontal" Margin="0,5,0,0">
            <StackPanel x:Name="PipeSegR3C1" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR3C2" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR3C3" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR3C4" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR3C5" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
            <StackPanel x:Name="PipeSegR3C6" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
         </StackPanel>
         <StackPanel x:Name="PipeSegRow4" Orientation="Horizontal" Margin="0,5,0,0">
             <StackPanel x:Name="PipeSegR4C1" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR4C2" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR4C3" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR4C4" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR4C5" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR4C6" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
         </StackPanel>
         <StackPanel x:Name="PipeSegRow5" Orientation="Horizontal" Margin="0,5,0,0">
             <StackPanel x:Name="PipeSegR5C1" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR5C2" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR5C3" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR5C4" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR5C5" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
             <StackPanel x:Name="PipeSegR5C6" Orientation="Vertical" Margin="5,0,0,0" Width="90"></StackPanel>
         </StackPanel>
     </StackPanel>
</GroupBox>

窗口中还有更多的人。我想要实现的目标是:从Checkbox的内容动态地在StackPanel行和列中放置新的List<T>。这就是我尝试的内容,但我不确定它是否会起作用,或者我是否以正确的方式来到这里。我想提前指出,List<T>最初使用的GroupBox不是上面的public void PopulateWindow() { int itr = 1; //TO SCROLL THROUGH THE ROWS AND COLUMNS var fecPipes = new FilteredElementCollectoR(m_pDoc); //GET ALL PIPES var lstRVTPipes = fecPipes.OfCategory(BuiltInCategory.OST_PipeCurves). OfClass(typeof(PipeType)); //FILTER RVT PIPES var lstMEPSizes = fecPipes.OfCategory(BuiltInCategory.OST_PipeCurves). OfClass(typeof(MEPSize)); //FILTER NOMINAL DIAMETERS var lstPipeSeg = fecPipes.OfCategory(BuiltInCategory.OST_PipeCurves). OfClass(typeof(PipeSegment)); //FILTER PIPE SEGMENTS foreach (PipeType pPipe in lstRVTPipes) { CheckBox cb = new CheckBox(); //CREATE NEW CHECK BOX cb.Height = 15; //SET CHECK BOX HEIGHT cb.Name = pPipe.FamilyName.ToString(); //SET THE NAME OF THE CHECK BOX cb.Content = pPipe.FamilyName.ToString(); //SET THE CONTENT DISPLAY OF THE CHECK BOX switch (itr) //PLACE THE CHECK BOX { case 1: cb.BindingGroup = RVTPipeR1C1.BindingGroup; //DOES THIS WORK? RVTPipeR1C1.DataContext = cb; //DOES THIS WORK? /* I'D LIKE TO DO SOMETHING LIKE: * RVTPipeR1C1.Add(cb); OR * cb.Location = RVTPipeR1C1.Location; * BUT THAT'S NOT A OPTION... :-( */ break; // THEN IT KEEPS GOING LIKE THIS FOR A WHILE... // // } itr++; } } 。抱歉让人感到困惑 - 只要知道他们基本上是一样的。

*fd = pipe_fds[0];

如果有人可以告诉我如何放置这些物品,或者我做了对错的话,我会非常感激!任何有用的东西的链接都很棒!谢谢!

2 个答案:

答案 0 :(得分:2)

要动态放置元素集合,您可以使用ItemsControl。您可以在此处阅读:https://msdn.microsoft.com/ru-ru/library/system.windows.controls.itemscontrol(v=vs.110).aspxhttps://professorweb.ru/my/WPF/binding_and_styles_WPF/level20/20_2.php以及使用它的简单示例:http://www.wpf-tutorial.com/list-controls/itemscontrol/

在代码中,您为元素创建ObservableCollection<T>,其中TViewModel CheckBox

对于ItemsControl,您必须将StackPanel设为ItemsControl.ItemsPanelItemsSource="{Binding Path=Elements}"(其中Elements是您的ObservableCollection<T>)和{{1}为您的ItemsControl.ItemTemplate添加DataTemplate

我希望,我的建议会对你有所帮助)

答案 1 :(得分:0)

您可以通过将UIElement添加到StackPanel的Children属性来将其添加到StackPanel:

PipeSegR1C1.Children.Add(cb);

StackPanel没有行和列的概念。它只是将子元素排列成一条水平或垂直方向的线条,具体取决于Orientation属性的值:https://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel(v=vs.110).aspx

如果要在由列和行组成的灵活网格区域中组织控件,可以使用Grid元素并向其添加ColumnDefinition和RowDefinitions。有关详细信息,请参阅MSDN:https://msdn.microsoft.com/en-us/library/system.windows.controls.grid(v=vs.110).aspx#Properties