我试图更改按钮点击事件中特定网格列和行的内容。我能够得到它的行和列,但我不能让它消失。
我在CreateLabel
按钮上创建了新的UserControl,并将其添加到特定的Grid
列和行。
这是XAML代码:
<Window x:Class="DHL.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DHL"
mc:Ignorable="d"
Title="DHL Parcel" Height="500" Width="1000">
<Window.Resources>
<Style x:Key="RoundedButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Foreground" Value="#FFCB1F1F"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1">
<ContentPresenter>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Effect">
<Setter.Value>
<DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Name="gridzik">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="0,0,0,5">
<Button Style="{StaticResource RoundedButton}" Name="CreateLabel"
Content="Crate consingment" HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
TextBlock.TextAlignment="Center"
Width="130" Click="CreateLabel_Click" />
</Grid>
<Grid Grid.Row="1" Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19*"/>
<ColumnDefinition Width="11*"/>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource RoundedButton}" Name="CheckLabels"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center" Content="Check consingment"
TextBlock.TextAlignment="Center"
Width="130" Grid.ColumnSpan="2" Margin="10,0" Click="CheckLabels_Click" />
</Grid>
</Grid>
<Grid Grid.Column="1" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
</Grid>
</Grid>
</Grid>
这里有代码:
private void CreateLabel_Click(object sender, RoutedEventArgs e)
{
CreateLabel cl = new CreateLabel();
gridzik.Children.Add(cl);
Grid.SetColumn(cl, 1);
Grid.SetRow(cl, 0);
cl.dok_idm_textBox.Focus();
}
我尝试使用CheckLabels
按钮时要清除Grid Grid.Column="1"
内容。以下是我尝试这样做的方法:
private void CheckLabels_Click(object sender, RoutedEventArgs e)
{
foreach (UIElement item in gridzik.Children)
{
if (Grid.GetColumn(item) == 1 && Grid.GetRow(item) == 0)
{
gridzik.Children.Remove(item);
break;
}
}
Label labelek = new Label();
labelek.Content = "JHEEK";
gridzik.Children.Add(labelek);
Grid.SetColumn(labelek, 1);
Grid.SetRow(labelek, 0);
}
我发现它已被删除,但它无法消除,我不知道为什么。有什么建议吗?
答案 0 :(得分:0)
如果您不需要动态内容,则可以使用2个网格并相应地设置其可见性属性
private void CheckLabels_Click(object sender, RoutedEventArgs e)
{
grid1.Visibility = Visibility.Visible;
grid2.Visibility = Visibility.Collapsed;
}