WPF DataGrid读取XML

XML文件

<Tree>
  <Item>
    <编号>编号</编号>
    <树冠>树冠半径</树冠>
    <树干>树干直径</树干>
    <树龄>树龄</树龄>
    <古树等级>古树等级</古树等级>
  </Item>
  <Item>
    <编号>T1</编号>
    <树冠>3.3m</树冠>
    <树干>22cm</树干>
    <树龄 />
    <古树等级 />
  </Item>
  <Item>
    <编号>T2</编号>
    <树冠>2.5m</树冠>
    <树干>20cm</树干>
    <树龄 />
    <古树等级 />
  </Item>
  </Tree>

前台

 <DataGrid Name="dgTree"    ItemsSource="{Binding}" AlternationCount="2" BorderThickness="0"  Background="#000" VerticalAlignment="Top" HorizontalScrollBarVisibility="Hidden" HeadersVisibility="None" AutoGenerateColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Center" GridLinesVisibility="None" >
                                <DataGrid.Columns>
                                    <DataGridTextColumn  Width="70"  Binding="{Binding 编号}" ElementStyle="{StaticResource dgCellTree}"  />
                                    <DataGridTextColumn  Width="70" Binding="{Binding 树冠}" ElementStyle="{StaticResource dgCellTree}"   />
                                    <DataGridTextColumn  Width="70" Binding="{Binding 树干}" ElementStyle="{StaticResource dgCellTree}"/>
                                    <DataGridTextColumn  Width="70" Binding="{Binding 树龄}" ElementStyle="{StaticResource dgCellTree}"/>
                                    <DataGridTextColumn  Width="70" Binding="{Binding 古树等级}" ElementStyle="{StaticResource dgCellTree}"/>
                                </DataGrid.Columns>
                                <!--表格隔行显示不同颜色-->
                                <DataGrid.RowStyle>
                                    <Style TargetType="{x:Type DataGridRow}">
                                        <Style.Triggers>
                                            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                                <Setter Property="Background" Value="#717171" />
                                            </Trigger>
                                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                                <Setter Property="Background" Value="#000" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGrid.RowStyle>
                                <DataGrid.Resources>
                                    <!--选中表格后,鼠标离开后表格背景色-->
                                    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#000"/>
                                </DataGrid.Resources>
                            </DataGrid>

后台读取XML

string filePath = System.Windows.Forms.Application.StartupPath + "/Resource/XML/" + tvData + "/" + tvData + ".xml";
            if (!File.Exists(filePath))
            {
                return;
            }
            else
            {
                //获取XML中的数据,返回给DataGrid
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.ReadXml(filePath);
                System.Data.DataTable dt = ds.Tables[0];
                mainWindow.dgTree.ItemsSource = dt.DefaultView;
                }

dgCellTree设置表格样式

 <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="dgCellRight" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Right" />
                <Setter Property ="Foreground" Value="#C0C0C0"/>
                <Setter Property ="Height" Value="20"/>
                <Setter Property ="Margin" Value="0,10,0,0"/>
            </Style>
            <Style x:Key="dgCellLeft" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Left"/>
                <Setter Property ="Foreground" Value="#FFFFFF"/>
                <Setter Property ="Margin" Value="20,10,0,0"/>
            </Style>
            <Style x:Key="dgCellTree" TargetType="TextBlock" >
                <Setter Property ="Height" Value="25"/>
                <Setter Property ="Foreground" Value="#FFFFFF"/>
                <Setter Property ="FontSize" Value="14"/>
                <Setter Property ="VerticalAlignment" Value="Center"/>
                <Setter Property ="HorizontalAlignment" Value="Center"/>
                <Setter Property ="Margin" Value="0,5,0,0"/>
            </Style>
            <Style x:Key="dgCell" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Center" />
                <Setter Property ="Foreground" Value="#fff"/>
                <Setter Property ="Height" Value="30"/>
                <Setter Property ="Margin" Value="0,10,0,0"/>
                <Setter Property ="FontSize" Value="16"/>

            </Style>
            
        </ResourceDictionary>
         </Window.Resources>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在WPF中,可以通过以下方式实现DataGrid的全选功能: 1. 首先,需要在XAML文件中创建一个CheckBox来表示全选功能。例如,可以在DataGrid的上方添加一个CheckBox控件,并设置一个命名,如"全选CheckBox"。 2. 接下来,在代码文件中,可以使用DataGrid的SelectionChanged事件来处理全选功能。在事件处理程序中,可以获取到全选CheckBox的状态,然后根据其状态来改变DataGrid中的所有行的选中状态。 3. 在SelectionChanged事件处理程序中,可以使用VisualTreeHelper来遍历DataGrid中的所有行,并根据全选CheckBox的状态改变每一行的选中状态。具体步骤如下: a. 首先,使用VisualTreeHelper.GetChildrenCount方法获取DataGrid中的所有子元素的数量。 b. 然后,使用VisualTreeHelper.GetChild方法来获取每个子元素。 c. 判断获取到的子元素是否为DataGridRow,如果是,则可以使用DataGrid的SelectedItems属性来判断其选中状态并进行更新。 4. 最后,需要在全选CheckBox的Checked和Unchecked事件中调用全选功能的实现代码。例如,可以在Checked事件中将所有行都选中,而在Unchecked事件中将所有行都取消选中。 综上所述,以上是使用WPF实现DataGrid全选功能的基本步骤。按照这种方法,可以实现DataGrid的全选功能,并根据全选CheckBox的状态来改变每一行的选中状态。可以将以上代码嵌入到WPF项目中相应的位置并且根据个人需求进行调整。 ### 回答2: 在WPFDataGrid中实现全选的方法如下所示: 1. 首先,我们需要在DataGrid控件中添加一个全选的复选框列。可以使用DataGridTemplateColumn来创建一个模板列,其中包含一个CheckBox控件。 2. 在XAML代码中,我们可以添加以下的代码片段: ```xaml <DataGrid x:Name="myDataGrid"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <CheckBox x:Name="chkSelectAll" Content="全选" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"/> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!-- 其他列 --> </DataGrid.Columns> </DataGrid> ``` 3. 在代码后台的.cs文件中,我们可以添加以下代码: ```csharp private void chkSelectAll_Checked(object sender, RoutedEventArgs e) { foreach (var item in myDataGrid.ItemsSource) { // 获取数据项中名为"IsSelected"的属性,并将其设置为true PropertyInfo propertyInfo = item.GetType().GetProperty("IsSelected"); propertyInfo.SetValue(item, true); } } private void chkSelectAll_Unchecked(object sender, RoutedEventArgs e) { foreach (var item in myDataGrid.ItemsSource) { PropertyInfo propertyInfo = item.GetType().GetProperty("IsSelected"); propertyInfo.SetValue(item, false); } } ``` 在这个示例中,我们通过遍历DataGrid中的每个数据项,使用反射来获取名为"IsSelected"的属性,并将其设置为true或false来实现全选或取消全选的功能。 请注意,上述示例假设数据项中有一个名为"IsSelected"的布尔属性来表示每一行是否被选中。您可以根据自己的需求调整代码。 ### 回答3: 要实现 WPF DataGrid 的全选功能,可以按照以下步骤进行操作: 1. 在 XAML 文件中,创建一个 DataGrid 控件,并指定其 Name 属性,以便在后面的代码中引用它。 ```xml <DataGrid x:Name="myDataGrid"> <!-- DataGrid 的列定义 --> </DataGrid> ``` 2. 在代码文件中,定义一个集合来存储 DataGrid 的数据源,并将其绑定到 DataGrid 控件。 ```csharp public ObservableCollection<MyDataModel> MyDataCollection { get; set; } myDataGrid.ItemsSource = MyDataCollection; ``` 3. 添加一个复选框作为全选按钮,并为其添加一个点击事件处理程序。 ```xml <CheckBox x:Name="selectAllCheckBox" Click="SelectAllCheckBox_Click" Content="全选" /> ``` 4. 在点击事件处理程序中,通过遍历 DataGrid 的行,选中或取消选中每一行的复选框。 ```csharp private void SelectAllCheckBox_Click(object sender, RoutedEventArgs e) { CheckBox checkBox = (CheckBox)sender; bool isChecked = checkBox.IsChecked ?? false; foreach (var item in MyDataCollection) { item.IsSelected = isChecked; } } ``` 5. 在 DataGrid 的列定义中,添加一个模板列,其中包含一个复选框,用于选中或取消选中每一行。 ```xml <DataGrid.Columns> <!-- 其他列定义 --> <DataGridTemplateColumn> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <!-- 全选按钮的复选框 --> <CheckBox Click="SelectAllCheckBox_Click" IsChecked="{Binding ElementName=selectAllCheckBox, Path=IsChecked, Mode=TwoWay}" /> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <!-- 行的复选框 --> <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> ``` 通过以上步骤,你可以在 WPF DataGrid 中实现全选功能。点击全选复选框时,所有行的复选框会被选中或取消选中,然后可以通过访问数据源的 IsSelected 属性来获取被选中的行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值