WPF-用右键单击显示/隐藏选定的列

时间:2018-12-05 17:25:40

标签: c# wpf datagridview datagrid

我想在WPF上对我的DataGrid进行显示和隐藏控制。我只想添加2个按钮(带有ContextMenu或其他功能),但我不知道该怎么做。

我只想从DataGrid中选择一列,当我单击“隐藏”按钮时,它将被隐藏。当我单击“显示”按钮时,它将再次显示。你能帮我吗?谢谢。

这是我的DataGrid代码;

gridview1.CanUserDeleteRows = false;
        gridview1.IsReadOnly = true;
        SQLiteConnection baglanti = new SQLiteConnection("Data Source=db/veritabani.s3db");
        baglanti.Open();
        string komut = "SELECT * FROM belgeler";
        SQLiteDataAdapter da = new SQLiteDataAdapter(komut, baglanti);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gridview1.ItemsSource = dt.DefaultView;

2 个答案:

答案 0 :(得分:0)

首先,我不确定您如何右键单击一列以将其隐藏后再次显示。

除此之外,您可以为ContextStripMenu尝试类似的操作,但是我不在PC上,因此无法确定它是否有效:

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu contextMenu = new ContextMenu();
        contextMenu.MenuItems.Add(new MenuItem("Hide"));
        contextMenu.MenuItems.Add(new MenuItem("Show"));
        contextMenu.ItemClicked += new ToolStripItemClickedEventHandler(contexMenu_ItemClicked);
        contextMenu.Show(dataGridView1, new Point(e.X, e.Y));

    }
}

这是您的事件处理程序:

void contexMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 
{
    int mouseOverColumn = dataGridView1.HitTest(e.X,e.Y).ColumnIndex;
    dataGridView1.Columns[mouseOverColumn].Visible = false;
}

答案 1 :(得分:0)

您可以这样做

注意:这远非完美,但您将了解如何解决问题

如果您遇到麻烦让我知道,那我会尽力帮助

<DataGrid x:Name="dataGrid">
    <DataGrid.Resources>
        <local:VisibilityToBooleanConverter x:Key="VisibilityToBooleanConverter"></local:VisibilityToBooleanConverter>
        <local:BindingProxy x:Key="BindingProxy" Data="{Binding ElementName=dataGrid, Path=Columns}"></local:BindingProxy>
    </DataGrid.Resources>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu ItemsSource="{Binding Source={StaticResource BindingProxy}, Path=Data}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="IsCheckable" Value="True"/>
                                <Setter Property="IsChecked" Value="{Binding Visibility,Converter={StaticResource VisibilityToBooleanConverter}}"/>
                                <Setter Property="Header" Value="{Binding Header}"/>
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    ...
</DataGrid>
public class VisibilityToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility visibility)
        {
            return visibility == Visibility.Visible;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool boolValue)
        {
            return (boolValue) ? Visibility.Visible : Visibility.Hidden;
        }
        return Visibility.Hidden;
    }
}