从网格中删除列并让其他列重新定位c#

时间:2015-09-16 15:16:13

标签: c# wpf grid

我有一个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>  

1 个答案:

答案 0 :(得分:0)

而不是Visibility.Hidden使他们成为Visibility.Collapsed

喜欢 -

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