如何在用户选择第一个网格后自动生成下一个网格
本文关键字:网格 自动生成 下一个 第一个 选择 用户 | 更新日期: 2023-09-27 18:07:17
我想问如何自动生成包含标签和组合框的网格。我想要的是,一旦用户在comboBox1中选择了一个选项,该方法将自动生成另一个网格(包含combobox2)等等。
下面是我目前拥有的代码,现在我很想创建一个事件驱动的方法来显示下一个网格,一旦IsSelected在当前的组合框中被调用。
<Grid x:Name="comp1" Margin="0,84,230,345">
<Label Content="Test Component 1:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,4,0,0"/>
</Grid>
<Grid x:Name="comp2" Margin="0,127,230,302" Visibility="Hidden">
<Label Content="Test Component 2:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,4,0,0"/>
</Grid>
<Grid x:Name="comp3" Margin="0,166,230,262" Visibility="Hidden">
<Label Content="Test Component 3:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,5,0,0"/>
</Grid>
<Grid x:Name="comp4" Margin="0,205,230,222" Visibility="Hidden">
<Label Content="Test Component 4:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB4" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,6,0,0"/>
</Grid>
<Grid x:Name="comp5" Margin="0,245,230,182" Visibility="Hidden">
<Label Content="Test Component 5:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB5" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,6,0,0"/>
</Grid>
<Grid x:Name="comp6" Margin="0,281,230,145" Visibility="Hidden">
<Label Content="Test Component 6:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,3,0,0"/>
<ComboBox x:Name="testCompCB6" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,10,0,-3"/>
</Grid>
<Grid x:Name="comp7" Margin="0,328,230,100" Visibility="Hidden">
<Label Content="Test Component 7:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB7" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,5,0,0"/>
</Grid>
<Grid x:Name="comp8" Margin="0,370,230,59" Visibility="Hidden">
<Label Content="Test Component 8:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,3,0,0"/>
</Grid>
<Grid x:Name="comp9" Margin="0,406,230,15" Visibility="Hidden">
<Label Content="Test Component 9:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testCompCB9" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Margin="134,12,0,0"/>
</Grid>
嗨,对于那些对此感兴趣的人,我使用以下简单的过程编码方法解决了它。
:
<StackPanel x:Name="testCompList" HorizontalAlignment="Left" Height="274" Margin="10,92,0,0" VerticalAlignment="Top" Width="361">
<Canvas x:Name="comp1" Margin="0,0,8,0" Height="26">
<Label Content="Test Component 1:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp2" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 2:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp3" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 3:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp4" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 4:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp4" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp5" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 5:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp5" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp6" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 6:" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Top="3"/>
<ComboBox x:Name="testComp6" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp7" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 7:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp7" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp8" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 8:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewRow"/>
</Canvas>
<Canvas x:Name="comp9" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Test Component 9:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="testComp9" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Test Component" Canvas.Left="134" Canvas.Top="5"/>
</Canvas>
</StackPanel>
后面的代码:
int i = 1;
private void addNewRow(object sender, EventArgs e) //all will invoke this generic method to display the visibility of the next
{
ComboBox selectedTest = (ComboBox)sender;
Canvas thisRow = (Canvas)selectedTest.Parent;
int index =Int32.Parse(thisRow.Name.Substring(thisRow.Name.Length-1, 1));
if (thisRow is Canvas && index==i) //needs to ensure that only the previous canvas can invoke the next canvas once selection is done
{
i++;
Canvas newRow = (Canvas)this.FindName("comp" + i);
newRow.Visibility = Visibility.Visible;
//add the test Component to a data structure like a list or something
}
}