我想问一下如何自动生成包含标签和comboBox的网格。我想要的是,一旦用户在comboBox1中选择了一个选项,该方法将自动生成另一个网格(包含组合框2),依此类推。
下面是我目前正在使用的代码,现在我很想创建一个事件驱动的方法,以便在当前的comboBox中调用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>
答案 0 :(得分:0)
对于那些对此感兴趣的人,我使用以下天真的程序编码方法解决了它。
对于xaml:
<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
}
}