1
我想讓一個網格有選項來選擇文本框中的玩家數量(最多六個)和輸入名稱。網格下方有「+」和「 - 」按鈕,用於添加或移除播放器(以兩個開頭)。一切正常,直到你添加第6個玩家,然後嘗試減去1個玩家。播放器6文本框和名稱輸入文本框保持可見狀態,取代播放器5。這裏是XAML設置可見性在網格中崩潰的奇怪問題(c#,xaml,UWP)
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="800" Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Headers-->
<TextBlock Style="{ThemeResource HeaderTextBlockStyle}" Grid.Row="0" Grid.Column="0" Text=""/>
<TextBlock Style="{ThemeResource HeaderTextBlockStyle}" Grid.Row="0" Grid.Column="1" Text="Name" Margin="20,0,20,0" TextAlignment="Center"/>
<TextBlock Name="PlayerOneTag" Text="Player 1" FontSize="30" Grid.Row="1" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left"/>
<TextBlock Name="PlayerTwoTag" Text="Player 2" FontSize="30" Grid.Row="2" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left"/>
<TextBlock Name="PlayerThreeTag" Text="Player 3" FontSize="30" Grid.Row="3" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerFourTag" Text="Player 4" FontSize="30" Grid.Row="4" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerFiveTag" Text="Player 5" FontSize="30" Grid.Row="5" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerSixTag" Text="Player 6" FontSize="30" Grid.Row="6" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBox Name="PlayerOneName" FontSize="30" Grid.Row="1" Grid.Column="1" />
<TextBox Name="PlayerTwoName" FontSize="30" Grid.Row="2" Grid.Column="1" />
<TextBox Name="PlayerThreeName" FontSize="30" Grid.Row="3" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerFourName" FontSize="30" Grid.Row="4" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerFiveName" FontSize="30" Grid.Row="5" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerSixName" FontSize="30" Grid.Row="6" Grid.Column="1" Visibility="Collapsed"/>
<StackPanel Name="PlayerTwoButtons" Grid.Row="7" Orientation="Horizontal">
<Button Name="AddPlayerThreeButton" Tapped="AddPlayerThree" Style="{StaticResource ResourceKey=PlusButton}" Visibility="Visible"/>
</StackPanel>
<StackPanel Name="PlayerThreeButtons" Visibility="Collapsed" Grid.Row="7" Orientation="Horizontal">
<Button Name="MinusPlayerThreeButton" Tapped="MinusPlayerThree" Style="{StaticResource ResourceKey=MinusButton}" />
<Button Name="AddPlayerFourButton" Tapped="AddPlayerFour" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerFourButtons" Visibility="Collapsed" Grid.Row="7" Orientation="Horizontal" >
<Button Name="MinusPlayerFourButton" Tapped="MinusPlayerFour" Style="{StaticResource ResourceKey=MinusButton}"/>
<Button Name="AddPlayerFiveButton" Tapped="AddPlayerFive" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerFiveButtons" Grid.Row="7" Orientation="Horizontal" Visibility="Collapsed">
<Button Name="MinusPlayerFiveButton" Tapped="MinusPlayerFive" Style="{StaticResource ResourceKey=MinusButton}" />
<Button Name="AddPlayerSixButton" Tapped="AddPlayerSix" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerSixButtons" Grid.Row="7" Orientation="Horizontal" Visibility="Collapsed">
<Button Name="MinusPlayerSixButton" Tapped="MinusPlayerSix" Style="{StaticResource ResourceKey=MinusButton}" />
</StackPanel>
</Grid>
這裏是背後
private void AddPlayerThree(object sender, TappedRoutedEventArgs e)
{
PlayerThreeName.Visibility = Visibility.Visible;
PlayerThreeTag.Visibility = Visibility.Visible;
PlayerTwoButtons.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Visible;
}
private void AddPlayerFour(object sender, TappedRoutedEventArgs e)
{
PlayerFourName.Visibility = Visibility.Visible;
PlayerFourTag.Visibility = Visibility.Visible;
PlayerThreeButtons.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Visible;
}
private void AddPlayerFive(object sender, TappedRoutedEventArgs e)
{
PlayerFiveName.Visibility = Visibility.Visible;
PlayerFiveTag.Visibility = Visibility.Visible;
PlayerFourButtons.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Visible;
}
private void AddPlayerSix(object sender, TappedRoutedEventArgs e)
{
PlayerSixName.Visibility = Visibility.Visible;
PlayerSixTag.Visibility = Visibility.Visible;
AddPlayerSixButton.Visibility = Visibility.Collapsed;
MinusPlayerSixButton.Visibility = Visibility.Visible;
}
private void MinusPlayerThree(object sender, TappedRoutedEventArgs e)
{
PlayerThreeName.Visibility = Visibility.Collapsed;
PlayerThreeTag.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Collapsed;
PlayerTwoButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerFour(object sender, TappedRoutedEventArgs e)
{
PlayerFourName.Visibility = Visibility.Collapsed;
PlayerFourTag.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerFive(object sender, TappedRoutedEventArgs e)
{
PlayerFiveName.Visibility = Visibility.Collapsed;
PlayerFiveTag.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerSix(object sender, TappedRoutedEventArgs e)
{
PlayerSixTag.Visibility = Visibility.Collapsed;
PlayerSixName.Visibility = Visibility.Collapsed;
PlayerSixButtons.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Visible;
}
關於如何解決此問題的任何想法的C#代碼,我將不勝感激!
唉謝謝!!我知道這將會是一件非常愚蠢的事情。我很感激! – robbiestells