我有一组XAML元素,如下所示:
<ComboBox x:Name="comB1" HorizontalAlignment="Left" Margin="244,90,0,0" VerticalAlignment="Top" Width="130" Height="48" Visibility="Collapsed">
<ComboBox x:Name="comB2" HorizontalAlignment="Left" Margin="244,180,0,0" VerticalAlignment="Top" Width="130" Height="48" Visibility="Collapsed">
<ComboBox x:Name="comB3" HorizontalAlignment="Left" Margin="244,270,0,0" VerticalAlignment="Top" Width="130" Height="48" Visibility="Collapsed">
<Button Content="+" HorizontalAlignment="Left" Margin="171,224,0,0" VerticalAlignment="Top" Height="51" Width="59" Click="Button_Click"/>
我想在每次单击此Visibility
按钮后将ComboBox
的{{1}}更改为visible
。
答案 0 :(得分:4)
此解决方案适用于ComboBoxes
的任何计数。试试这个:
int num = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (num <= ComboCounts)//ComboCounts is count of your ComboBoxes
{
((ComboBox)this.FindName("comB" + num)).Visibility = Visibility.Visible;
num++;
}
}
答案 1 :(得分:3)
int ComboNO = 0;
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
ComboNO++;
for (int i = 0; i < ComboCount; i++)
if (ComboNO == i)
foreach (FrameworkElement item in RootElement.Children)
if (item.Name == "comB" + i) item.Visibility = Visibility.Visible;
}
答案 2 :(得分:2)
这是一个技巧:
- 在类
的范围内声明一个变量int numOfVisibleCbx = 0;
- 点击按钮
if(numOfVisibleCbx == 0)
comB1.Visibility = Visibility.Visible;
else if(numOfVisibleCbx == 1)
comB2.Visibility = Visibility.Visible;
else if(numOfVisibleCbx == 2)
comB3.Visibility = Visibility.Visible;
numOfVisibleCbx++;
答案 3 :(得分:0)
如果你想隐藏其他cbx,那么
这是一个技巧:
- 在类
的范围内声明一个变量int numOfVisibleCbx = 0;
- 功能
void hideAll()
{
comB1.Visibility = Visibility.Hidden;
comB2.Visibility = Visibility.Hidden;
comB3.Visibility = Visibility.Hidden;
}
- 点击按钮
hideAll();
if(numOfVisibleCbx == 0)
comB1.Visibility = Visibility.Visible;
else if(numOfVisibleCbx == 1)
comB2.Visibility = Visibility.Visible;
else if(numOfVisibleCbx == 2){
comB3.Visibility = Visibility.Visible;
numOfVisibleCbx = 0;
}
numOfVisibleCbx++;
答案 4 :(得分:0)
尝试一下:
public MainWindow()
{
InitializeComponent();
_comBs =
this
.Root
.Children
.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("comB"))
.OrderBy(x => int.Parse(x.Name.Substring("comB".Length)))
.ToArray();
}
private ComboBox[] _comBs = null;
private int _comB_index = -1;
private void Button_Click(object sender, RoutedEventArgs e)
{
_comB_index++;
for (var i = 0; i < _comBs.Length; i++)
{
_comBs[i].Visibility = i == _comB_index % _comBs.Length
? Visibility.Visible
: Visibility.Collapsed;
}
}
您必须将this.Root
替换为容器元素。
我已经测试了这个解决方案,但它确实有效。