简体   繁体   English

WPF DataGrid ContextMenu数据传输

[英]WPF DataGrid ContextMenu data transfer

Good day to everyone! 大家好! I'm new into WPF and .NET and trying to make serious application in it. 我是WPF和.NET的新手,并试图在其中进行认真的应用。 In part, that I can't figure how to make right, I have Grid that contains TabControl with some tabs, on every tab I have DataGrid with data from MySQL. 在某种程度上,我无法弄清楚该怎么做,我有一个包含带有某些选项卡的TabControl Grid ,在每个选项卡上,我都有一个DataGrid和MySQL的数据。 This part is not so difficult, but now I need to make some filter, that enables/disables some columns by user choice. 这部分并不是那么困难,但是现在我需要做一些过滤器,通过用户选择启用/禁用某些列。 For that I use ContextMenu that on calls on click new window with list of column names. 为此,我使用ContextMenu在带有列名列表的单击新窗口的调用上。 On every tab. 在每个选项卡上。 So I made universal Filter_Window . 所以我做了通用的Filter_Window I figured how to get this names, but can't get the DataGrid , that calls for Filter_Window . 我想出了如何获取此名称,但无法获取需要Filter_WindowDataGrid I tried VisualTreeHelper , but ContextMenu have it's own VisualTree and it's gave me nothing. 我尝试了VisualTreeHelper ,但是ContextMenu拥有它自己的VisualTree,却没有给我任何东西。 DataContext gave nothing eather. DataContext没有提供任何帮助。
Just don't want to make Click for every Tab separately, it's not right, but it's most obvious solution. 只是不想单独为每个选项卡单击,这是不对的,但这是最明显的解决方案。 Just need the way to transfer DataGrid to Filter_Window . 只需要将DataGrid传输到Filter_Window的方法即可。
Sorry for my language, if I name things bad. 如果我说的不好,对不起我的语言。 If code samples needed to clarify what I written - say and I'll post some. 如果代码示例需要澄清我写的内容,请说出来,然后我将发布一些内容。

Create a class with boolean properties of all columns of your DataGrid and choose them in your new window. 创建一个具有DataGrid所有列的boolean属性的类,然后在新窗口中选择它们。 For example: 例如:

public class ColumnChooser
{        
    public bool IsShowIdStudent { get; set; }
    public bool IsShowFirstName { get; set; }
    public bool IsShowLastName { get; set; }
    public bool IsShowGroup { get; set; }
    public bool IsShowUniversity { get; set; }
}

And after that you can set Visibility for your column based on the class which is storing your boolean values like that: 然后,您可以根据存储布尔值的类,为列设置Visibility

YourDataGrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed;

Did found more suitable for me answer, but forgot to post it here. 确实找到了更适合我的答案,但忘了在这里发布。 Done with Command and CommandParameter. 用Command和CommandParameter完成。 What I did in XAML in MainWindow: 我在MainWindow中的XAML中所做的事情:

<DataGrid.ContextMenu>
  <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
    <MenuItem x:Name ="AktFilter" Header="_Фильтры..." Command="{StaticResource ResourceKey=FilterCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}"/>
  </ContextMenu>
</DataGrid.ContextMenu>

and added new Command into project: 并将新的Command添加到项目中:

class FilterCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged
    public void Execute(object parameter)
    {
        if (parameter != null)
        {
            FilterWindow newW = new FilterWindow();
            newW.Owner = Application.Current.MainWindow;
            newW._sender = parameter as DataGrid;
            newW.ShowDialog();//можно через условие нажатия на ок попробовать запилить обработку сохранения или !!!!! смену состояния строк
            if (newW.DialogResult == true)
            {
                DataGrid dg = parameter as DataGrid;
                Filters filterList = (Filters)newW.FilterDG.ItemsSource;
                for (int i = 0; i < dg.Columns.Count; i++)
                {
                    if (filterList[i].Visible)
                    {
                        dg.Columns[i].Visibility = Visibility.Visible;
                    }
                    else
                    {
                        dg.Columns[i].Visibility = Visibility.Collapsed;
                    }
                    //тут еще засунуть фильтры!!!
                }
            }
        }
    }
}  

This code generates DataGrid from column names and checkboxes, changes only applied while running program, save of Visibility is not done yet... Was too lazy, got back to program only now. 这段代码从列名和复选框生成DataGrid,仅在运行程序时应用更改,还没有保存可见性...太懒了,现在才返回程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM