从网格中删除列,并重新定位其他列

本文关键字:定位 其他 新定位 网格 删除列 | 更新日期: 2023-09-27 18:13:11

我有一个wpf应用程序,我希望能够从网格中删除一列,然后有其他列重新定位自己(即,如果用户删除第一列所有其他列移动到左边,如果用户删除第二列所有其他列后第二个移动到左边)通过点击按钮的事件。目前我只能隐藏列中的项目,它留下了一个空白,这是我不想要的。

private void SetColumnWidthCol1()
    {
        Col2.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
        Col4.Width = new GridLength(150);
    }
    private void SetColumnWidthCol2()
    {
        Col1.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
        Col4.Width = new GridLength(150);
    }
    private void SetColumnWidthCol3()
    {
        Col1.Width = new GridLength(150);
        Col2.Width = new GridLength(150);
        Col4.Width = new GridLength(150);
    }
    private void SetColumnWidthCol4()
    {
        Col1.Width = new GridLength(150);
        Col2.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
        Col5.Width = new GridLength(150);
        Col6.Width = new GridLength(150);
    }
    private void SetColumnWidthCol5()
    {
        Col1.Width = new GridLength(150);
        Col2.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
        Col4.Width = new GridLength(150);
        Col6.Width = new GridLength(150);
    }
    private void SetColumnWidthCol6()
    {
        Col1.Width = new GridLength(150);
        Col2.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
        Col4.Width = new GridLength(150);
        Col5.Width = new GridLength(150);
    }
    private void CloseRock_Click(object sender, RoutedEventArgs e)
    {
        RockLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseRock.Visibility = System.Windows.Visibility.Hidden;
    }
    private void CloseContacts_Click(object sender, RoutedEventArgs e)
    {
        ContactsLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseContacts.Visibility = System.Windows.Visibility.Hidden;
    }
    private void CloseFluid_Click(object sender, RoutedEventArgs e)
    {
        FluidLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseFluid.Visibility = System.Windows.Visibility.Hidden;
    }
    private void CloseRegions_Click(object sender, RoutedEventArgs e)
    {
        RegionsLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseRegions.Visibility = System.Windows.Visibility.Hidden;
    }
    private void CloseProbabilities_Click(object sender, RoutedEventArgs e)
    { 
        ProbabilitiesLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseProbabilities.Visibility = System.Windows.Visibility.Hidden;
    }
    private void CloseEconomics_Click(object sender, RoutedEventArgs e)
    {
        EconomicsLabel.Visibility = System.Windows.Visibility.Hidden;
        CloseEconomics.Visibility = System.Windows.Visibility.Hidden;
    }
}

XAML

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
x:Class="ItemWrapPanel.MainWindow"
Title="MainWindow" Height="350" Width="1000">
<Window.Resources>
<LinearGradientBrush x:Key="LabelBackground" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Color="#FFEFEEEE" Offset="0"/>
    <GradientStop Color="#E7E7E7E7" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="selectedHeaderBackground" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Color="#F6CD1D" Offset="0"/>
    <GradientStop Color="#EBA32A" Offset="1"/>
</LinearGradientBrush>
</Window.Resources>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
    <RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition x:Name="Col1"/>
    <ColumnDefinition x:Name="Col2"/>
    <ColumnDefinition x:Name="Col3"/>
    <ColumnDefinition x:Name="Col4"/>
    <ColumnDefinition x:Name="Col5"/>
    <ColumnDefinition x:Name="Col6"/>
</Grid.ColumnDefinitions>
<Label x:Name="RockLabel"
    Background="{StaticResource LabelBackground}"
    Content="Rock Properties"
    Grid.Row="0"
    Grid.Column="0"
    MouseUp="RockLabel_MouseDoubleClick"
    VerticalAlignment="Top"/>
<Label x:Name="ContactsLabel"
    Content="Contacts"
    Background="{StaticResource LabelBackground}"
    Grid.Column="1"
    Grid.Row="0"
    MouseUp="ContactsLabel_MouseDoubleClick"
    VerticalAlignment="Top"/>
<Label x:Name="FluidLabel"
    Content="Fluid Properties"
    Background="{StaticResource LabelBackground}"
    Grid.Column="2"
    Grid.Row="0"
    MouseUp="FluidLabel_MouseDoubleClick" 
    VerticalAlignment="Top"/>
<Label x:Name="RegionsLabel"
    Content="Regions"
    Background="{StaticResource LabelBackground}"
    Grid.Column="3"
    Grid.Row="0"
    MouseUp="RegionsLabel_MouseDoubleClick" 
    VerticalAlignment="Top"/>
<Label x:Name="ProbabilitiesLabel"
    Content="Probabilities"
    Background="{StaticResource LabelBackground}"
    Grid.Column="4"
    Grid.Row="0"
    MouseUp="ProbabiltitesLabel_MouseDoubleClick"
    VerticalAlignment="Top"/>
<Label x:Name="EconomicsLabel"
    Content="Economics"
    Background="{StaticResource LabelBackground}"
    Grid.Column="5"
    Grid.Row="0"
    MouseUp="EconomicsLabel_MouseDoubleClick" 
    VerticalAlignment="Top"/>
<Button x:Name="Resetbtn" Content="Restore" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Reset_Click" Margin="0,59,0,0"/>
<Button Name="CloseRock" Content="X" HorizontalAlignment="Right" Margin="0,2,0,0" VerticalAlignment="Top" Width="24" Grid.Column="0" Grid.Row="0" Click="CloseRock_Click"/>
<Button Name="CloseContacts" Content="X" HorizontalAlignment="Right" Margin="0,2,2,0" VerticalAlignment="Top" Width="24" Grid.Column="1" Grid.Row="0" Click="CloseContacts_Click"/>
<Button Name="CloseFluid" Content="X" HorizontalAlignment="Right" Margin="0,2,2,0" VerticalAlignment="Top" Width="24" Grid.Column="2" Grid.Row="0" Click="CloseFluid_Click"/>
<Button Name="CloseRegions" Content="X" HorizontalAlignment="Right" Margin="0,2,1,0" VerticalAlignment="Top" Width="24" Grid.Column="3" Grid.Row="0" Click="CloseRegions_Click"/>
<Button Name="CloseProbabilities" Content="X" HorizontalAlignment="Right" Margin="0,2,1,0" VerticalAlignment="Top" Width="24" Grid.Column="4" Grid.Row="0" Click="CloseProbabilities_Click"/>
<Button Name="CloseEconomics" Content="X" HorizontalAlignment="Right" Margin="0,2,1,0" VerticalAlignment="Top" Width="24" Grid.Column="5" Grid.Row="0" Click="CloseEconomics_Click"/>
</Grid>  

从网格中删除列,并重新定位其他列

Visibility.Hidden改为Visibility.Collapsed

,

private void CloseContacts_Click(object sender, RoutedEventArgs e)
{
    ContactsLabel.Visibility = System.Windows.Visibility.Collapsed;
    CloseContacts.Visibility = System.Windows.Visibility.Collapsed;
}