WPF4.5 Introduction to the 24, "DataGrid Control Part 2"

22 篇文章 0 订阅

refs:

http://blog.okazuki.jp/entry/20130224/1361693816


Available columns in a DataGrid

In DataGrid, but you can display the data easily by using the customization of the auto-generation features and events of the column, the corresponding and to a simple case, to develop a display function of the general-purpose data using the attribute other than the case, will now be introduced, how to define a column by yourself is common. The following the typical things in the available column in the DataGrid.

Class nameDescription
DataGridTextBoxColumnIt displays the data specified in the Binding property as text. In edit mode, to display the text box.
DataGridCheckBoxColumnIt displays the data specified in the Binding property as a check box.
DataGridComboBoxColumnThe combo box to display the collection set to ItemsSource property as a choice and then displayed in the cell. And properties to be displayed in the combo box in the DisplayMemberPath properties and SelectedValuePath property to set the property to be treated as the selected value. Data to be displayed, you can correspondence with those from the selected element in the collection set in the ItemsSource property in SelectedValueBinding property value of the property specified in the SelectedValuePath, being selected by the collection set to ItemsSource property in SelectedItemBinding property.
DataGridTemplateColumnYou can freely customize the display content using DataTemplate. You can customize the display of the most flexible cells in the column that can be specified in the DataGrid.

Column of the DataGrid, you can customize the contents of the header by using the following properties.

PropertyDescription
object Header {get; set;}Gets or sets the value of the column header.
DataTemplate HeaderTemplate {get; set;}Gets or sets the template for displaying the value of the column header.
Style HeaderStyle {get; set;}Gets or sets the style that is used to display the header of the column. Type to apply the style is DataGridHeaderColumn class.
Example of the use of DataGrid column

Using the columns of the DataGrid to display the data in the DataGrid. First, place the DataGrid on the screen. Because this time you do not want to use auto-generated columns of the DataGrid, you have to disable the auto-generation function to the AutoGeneratedColumns property to False.

  <Window x: Class = "DataGridSample03.MainWindow "
          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns: local = "clr-namespace : DataGridSample03"
          Title = "MainWindow"    Height = "350"    Width = "525">
     <Grid>
         <DataGrid Name = "dataGrid"    AutoGenerateColumns = "False">
         </ DataGrid>
     </ Grid>
 </ Window>

Then, you define a class to be displayed in the DataGrid. Create a Person class with an enumerated type and the type bool property of the string type as follows.

  // Enum representing the gender
 public enum Gender
 {
     None,
     Men,
     Women
 }
 
 // Data to be displayed in the DataGrid
 public class Person
 {
     // name
     public string Name {get; set; }
     // sex
     public Gender Gender {get; set; }
     // Whether the authenticated user
     public bool AuthMember {get; set; }
 }

In the constructor of MainWindow, and set the list of Person class to DataGrid ItemsSource property of.

  // Generate review appropriate data 100
 var data = new ObservableCollection <Person> (
     Enumerable.Range (1, 100) .Select ( i => new Person
     {
         Name = "Taro Tanaka" + i,
         Gender = i% 2 == 0 Gender.Men :? Gender.Women,
         AuthMember = i% 5 == 0
     }));
 // Set to DataGrid
 this .dataGrid.ItemsSource = data;

Now that was ready to display data will continue to display the data to define the column.

Use of DataGridTextBoxColumn and DataGridCheckBoxColumn

You define a column to the DataGrid. Column in the DataGrid, set the Columns property. The DtaGridTextColumn the Name property to bind the DataGridCheckBoxColumn to AuthMembe property.

  <DataGrid Name = "dataGrid"    AutoGenerateColumns = "False">
     <DataGrid. Columns>
         <DataGridTextColumn Header = "name"    Binding = "{Binding Name}" />
         <DataGridCheckBoxColumn Header = "authenticated"    Binding = "{Binding AuthMember}" />
     </ DataGrid. Columns>
 </ DataGrid>

When you run, it will be as follows.
f:id:okazuki:20130224171139p:image

Press the F2 key to select the cell, you can cell editing by clicking on the cell in the selection. The figure below is the image you are editing a column name.

f:id:okazuki:20130224171227p:image

Use of DataGridComboBoxColumn

It then displayed using the DataGridComboBoxColumn the Gender property. In auto-generated, since the value of the enum has been displayed as it is, here is to be able to display a Japanese label. First, create a class with the data to be displayed on the ComboBox. And string for the label, is a simple class that has the value of the corresponding Gender.

  namespace DataGridSample03
 {
     // Class to display the Gender in ComboBox
     public class GenderComboBoxItem
     {
         // Label for display
         public string Label {get; set; }
         // value
         public Gender Value {get; set; }
     }
 }

Set the Label and Value to set the sequence of this class to the ItemsSource property of DataGridComboBoxColumn DisplayMemberPath and SelectedValuePath. We want association string the Gender property values ​​and the Person class of the selected element to set the binding of the Gender in SelectedValueBinding property.
So far in the non-appearance of the x: Array tag is intended to be defined in XAML an array of those specified in the Type attribute.

  <DataGridComboBoxColumn Header = "gender"  
                          SelectedValueBinding = "{Binding Gender}"
                          DisplayMemberPath = "Label"  
                          SelectedValuePath = "Value">
     <DataGridComboBoxColumn. ItemsSource>
         <X: Array Type = "{ x: Type local: GenderComboBoxItem}">
             <Local: GenderComboBoxItem Label = "unselected"    Value = "None" />
             <Local: GenderComboBoxItem Label = "man"    Value = "Men" />
             <Local: GenderComboBoxItem Label = "F"    Value = "Women" />
         </ X: Array>
     </ DataGridComboBoxColumn. ItemsSource>
 </ DataGridComboBoxColumn>

The display of the DataGrid you add this definition is shown below. And display data, you can see that the data in the drop-down in the editing is in Japanese.

f:id:okazuki:20130224171427p:image

Use of DataGridTemplateColumn

Then, use the DataGridTemplateColumn that you can freely customize the display of the cell. Using the StackPanel to the template of the header and the cell displays a plurality of items. The display contents specified in HeaderTemplate using HeaderStyle has been specified in the horizontal direction full. Without this setting, the display of the header will become a left-justified. The display is in CellTemplate, and set the UI for editing the CellEditingTemplate. Which templates are also set to display a vertical as well as multiple elements in the StackPanel. Between each element has to display the borders with the Separator control.

  <DataGridTemplateColumn>
     <DataGridTemplateColumn. HeaderStyle>
         <Style TargetType = "DataGridColumnHeader">
             <Setter Property = "HorizontalContentAlignment"    Value = "Stretch" />
         </ Style>
     </ DataGridTemplateColumn. HeaderStyle>
     <DataGridTemplateColumn. HeaderTemplate>
         <DataTemplate>
             <StackPanel>
                 <TextBlock Text = "name" />
                 <Separator />
                 <TextBlock Text = "authenticated" />
             </ StackPanel>
         </ DataTemplate>
     </ DataGridTemplateColumn. HeaderTemplate>
     <DataGridTemplateColumn. CellTemplate>
         <DataTemplate>
             <StackPanel>
                 <TextBlock Text = "{Binding Name }" />
                 <Separator />
                 <CheckBox IsEnabled = "False"    IsChecked = "{Binding AuthMember}" />
             </ StackPanel>
         </ DataTemplate>
     </ DataGridTemplateColumn. CellTemplate>
     <DataGridTemplateColumn. CellEditingTemplate>
         <DataTemplate>
             <StackPanel>
                 <TextBox Text = "{Binding Name }" />
                 <Separator />
                 <CheckBox IsChecked = "{Binding AuthMember }" />
             </ StackPanel>
         </ DataTemplate>
     </ DataGridTemplateColumn. CellEditingTemplate>
 </ DataGridTemplateColumn>

Run the results will be as follows. You can see that you are able to display two lines of data in a single cell by using the template.

f:id:okazuki:20130224171601p:image




在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值