none
wpf 点击DataGrid列头立刻选中此列 RRS feed

全部回复

  • 点击列头 整列被选中,选中颜色自定义

    Hi   ly6702,

    你可以参考下面的实现思路。在列表头中实现: 选中row的时候获取去当前row 改变样式

    wpf DataGrid CheckBox列全选
    https://www.cnblogs.com/renzhendewo/p/3161312.html

    WPF DataGrid表头Checkbox 全选和反选
    https://blog.csdn.net/wumingwuxin/article/details/73657919

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月22日 6:14
    版主
  • 定义DataGridCell如下

    <Style TargetType="DataGridCell" x:Key="dgc">
           
                <Setter Property="Tag" >
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource T2}">
                             <!--此处绑定为选择的列头项-->
                            <Binding  Path="DataContext.SelectColumn" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=DataGrid}"/>
                            <!--此处绑定为列头的名称-->
                            <Binding  Path="Column.Header" RelativeSource="{RelativeSource Mode=Self}" />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Tag,RelativeSource={RelativeSource Mode=Self}}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

    定义多路转换器如下

     public class ToConverter : IMultiValueConverter
        {
            
          
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                var a = values[0].ToString();
                var b = values[1].ToString();
    
                return a == b ? true : false;
    
            }
    
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    并在xaml页面引用

    <local:ToConverter    x:Key="T2" />

    定义DataGrid并创建排序事件和使用CellStyle如下

       <DataGrid Sorting="Dg_Sorting"   ItemsSource="{Binding List}"  CellStyle="{StaticResource dgc}"     x:Name="dg"  />


    定义数据类模型如下

    public class T1
        {
            public string  Name { get; set; }
            public int ID { get; set; }
            
        }
        public class T2:INotifyPropertyChanged
        {
            public ObservableCollection<T1> List { get; set; }
            private string select;
            public string SelectColumn { get=>select; set { select = value;onchanged(new PropertyChangedEventArgs("SelectColumn")); } }
            protected void onchanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
            public event PropertyChangedEventHandler PropertyChanged;
        }


    使用并将其赋值给DataGrid的DataContext如下

     ObservableCollection<T1> t = new ObservableCollection<T1>();
            T2 datalist;
            public MainWindow()
            {
                InitializeComponent();
    
    
                for (var i = 0; i < 20; i++)
                    t.Add(new T1() { ID = i, Name = "i: " + i.ToString() });
                datalist = new T2();
                datalist.List = t;
                datalist.SelectColumn = string.Empty;
                dg.DataContext = datalist;
    
    
            }


    在DataGrid的排序时间中编写代码如下

    private void Dg_Sorting(object sender, DataGridSortingEventArgs e)
            {
                datalist.SelectColumn = e.Column.Header.ToString();
                e.Handled = true;
            }


    运行结果如下

    //////////////////////////////////////////////////////////////////////////

    以上是UI界面

    如果是在SelectCells中添加数据,我建议是关闭DataGrid的虚拟化代码如下

    VirtualizingPanel.IsVirtualizing="False"

    但是在多行数据下DataGrid的性能会有下降

    然后添加博客园玖彩技术团队的这篇文章中的扩展类

    然后在排序事件下编写代码如下

    private void Dg_Sorting(object sender, DataGridSortingEventArgs e) { datalist.SelectColumn = e.Column.Header.ToString(); for (var i = 0; i < datalist.List.Count; i++) dg.SelectedCells.Add(new DataGridCellInfo(dg.GetCell(i, e.Column.DisplayIndex))); e.Handled = true; }

    我个人是强力不建议这么操作。

    我的建议是通过绑定的方式,操作数据,在UI绑定来修改datagrid的数据,

    一是避免关闭datagrid的虚拟化

    二是操作上相对简单,不需要太多复杂的东西


    • 已编辑 ARM830 2019年8月22日 9:06
    2019年8月22日 8:50