none
DataGrid对应列的“Header”属性 RRS feed

  • 问题

  • DataGrid在绑定“数据源”之后,它对应的“列”的名称就不能改变了吗???

    如下图:

    改变列名称的代码如下:

     var datasource = GetEnumerable(dicList).ToDataSource();
                                dgrid.ItemsSource = GetEnumerable(dicList).ToDataSource();

                                for (int i = 1; i < dgrid.Columns.Count; i++)
                                {
                                    //dgrid.Columns[1].Header = dgrid.Columns[1].Header.ToString().Replace("H", "");
                                    dgrid.Columns[i].Header = i.ToString();
                                }

                                this.mainPanel.Children.Add(dgrid);

    问题:希望将列名中的“H”去掉,应该怎样修改???     数据集已经绑定到“DataGrid”了


    Science and technology is my lover.

    2012年7月31日 12:26

答案

  • 你可以尝试使用converter,具体轻看我的代码:

    .xaml

    <UserControl x:Class="SilverlightTestApp.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SilverlightTestApp"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
        <UserControl.Resources>
            <local:BuildingConverter x:Key="BuildingConverter"></local:BuildingConverter>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot">
            <sdk:DataGrid AutoGenerateColumns="False" Name="dataGrid1">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="大楼名" Binding="{Binding BuildingName, Converter={StaticResource BuildingConverter}}">
                    </sdk:DataGridTextColumn>
                    <sdk:DataGridTextColumn Header="大楼编号" Binding="{Binding BuildingNO}">
                    </sdk:DataGridTextColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
        </Grid>
    
    </UserControl>

    .xaml.cs

        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                List<Building> buildings = new List<Building>();
                for (int i = 101; i < 125; i += 2)
                {
                    buildings.Add(new Building
                    {
                        BuildingName = "H" + i,
                        BuildingNO = (i * 11).ToString()
                    });
                }
                buildings.Add(new Building
                {
                    BuildingName = "X2012",
                    BuildingNO = "22132"
                });
                dataGrid1.ItemsSource = buildings;
            }
        }
        public class Building
        {
            public String BuildingName { get; set; }
            public String BuildingNO { get; set; }
        }

    BuildingConverter.cs

        public class BuildingConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value is String)
                {
                    String paramStr = value as String;
                    if (paramStr.StartsWith("H"))
                        return paramStr.Substring(1);
                }
                return value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    这样就可以实现你想要得效果。
    2012年8月2日 10:37
    版主