其他分享
首页 > 其他分享> > WPF DataGrid操作

WPF DataGrid操作

作者:互联网

AutoGenerateColumns     自动生成列

GridLinesVisibility     网格线可见性

CanUserDeleteRows   用户是否可以删除行

CanUserAddRows   用户是否可以添加行

文字居中

<Style x:Key="CenterAlignmentStyle" TargetType="TextBlock">
    <Setter Property="TextAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

<DataGridTextColumn Header="工步号" Width="50" Binding="{Binding StepID}" IsReadOnly="True" ElementStyle="{StaticResource CenterAlignmentStyle }"/>

 

右键选中

// 右键点击触发事件
MouseRightButtonDown="DataGrid_MouseRightButtonDown" 
// 只能选中一行
SelectionMode="Single"
private void DataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 获取坐标
            Point p = e.GetPosition((ItemsControl)sender);
            //  通过指定 Point 返回命中测试的最顶层 Visual 对象。                                               
            HitTestResult htr = VisualTreeHelper.HitTest((ItemsControl)sender, p);
            TextBlock o = htr.VisualHit as TextBlock;
            if (o != null)
            {
                DataGridRow dgr = GetParentObject<DataGridRow>(o) as DataGridRow;
                dgr.Focus();
                dgr.IsSelected = true;
            }
        }
        public T GetParentObject<T>(DependencyObject obj) where T : FrameworkElement
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);

            while (parent != null)
            {
                if (parent is T)
                {
                    return (T)parent;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return null;
        }

 

 

自定义列 不需要双击选择

<DataGridTemplateColumn Header="测试项" SortMemberPath="IsSelected" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}"
                                          HorizontalAlignment="Center"
                                          Command="{Binding Path=DataContext.SelectTestItemCommand ,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"
                                          Content="{Binding ItemName}"/>
                                
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

 

 

 

模板

            <DataGrid AutoGenerateColumns="False" GridLinesVisibility="None" CanUserDeleteRows="False"
                      CanUserAddRows="False" FontSize="13" ItemsSource="{Binding TestList}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="序号" Binding="{Binding ID}" Width="50"/>
                    <DataGridTemplateColumn Header="测试项" SortMemberPath="IsSelected" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}"
                                          HorizontalAlignment="Center"
                                          Command="{Binding Path=DataContext.SelectTestItemCommand ,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"
                                          Content="{Binding ItemName}"/>
                                
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--<DataGridTextColumn Header="测试次数"  Binding="{Binding Number}" Width="50"/>-->
                </DataGrid.Columns>
            </DataGrid>

 

标签:dgr,parent,DataGrid,操作,WPF,null,VisualTreeHelper,MouseRightButtonDown
来源: https://www.cnblogs.com/EndlessLife/p/14863779.html