如何以编程方式创建列表视图到网格

时间:2012-11-12 13:42:00

标签: c# wpf listview

我有一个定义了列和行的网格。通过知道x数量的项目,我可以将它添加到我的xaml没问题。不幸的是,我想以编程方式创建listview,因为我想根据执行SP时获得的x项目填充它们。这是我的xaml。标签也用于从SP结果中分配给定内容。有人能告诉我如何以编程方式创建这个吗?

<Grid Name="grdItems" Width="939" Grid.Row="1" HorizontalAlignment="Left" DataContext="{Binding}" Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="5"></RowDefinition>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="5"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="231"></ColumnDefinition>
        <ColumnDefinition Width="5"></ColumnDefinition>
        <ColumnDefinition Width="231"></ColumnDefinition>
        <ColumnDefinition Width="5"></ColumnDefinition>
        <ColumnDefinition Width="231"></ColumnDefinition>
        <ColumnDefinition Width="5"></ColumnDefinition>
        <ColumnDefinition Width="231"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Label Name="lblItem" Grid.Row="0" Grid.Column="0" Content="item label" />
    <ListView Name="lstFirstItem" HorizontalAlignment="Left" VerticalAlignment="Top" 
                    Height="auto" Margin="0,0,0,0" ItemsSource="" FontWeight="Regular" 
                    Grid.Column="0" Grid.Row="1">
        <ListView.View>
            <GridView>
                <GridView.ColumnHeaderContainerStyle>
                    <Style>
                        <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
                    </Style>
                </GridView.ColumnHeaderContainerStyle>
                <GridViewColumn Width="58" DisplayMemberBinding="{Binding ItemName}"></GridViewColumn>
                <GridViewColumn Width="174.25" DisplayMemberBinding="{Binding ItemDescription}"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

2 个答案:

答案 0 :(得分:2)

我们可以从代码后面动态添加行或列。

为了添加行:

    GridLength rowheight = new GridLength(100);  //your own value we can give *,Auto as well  
    RowDefinition rowDef = new RowDefinition {Height = rowheight };    
    mainGrid.RowDefinitions.Add(rowDef);

为了添加colomns:

    GridLength columnwidth = new GridLength(100);        
    ColumnDefinition colDef = new ColumnDefinition { Width = columnwidth};        
    mainGrid.ColumnDefinitions.Add(colDef );    

为了在特定行,列

添加文本块
    TextBlock textBlock = new TextBlock();        
    textBlock.Text = "Some Value";       
    Grid.SetRow(textBlock, rowNum);        
    Grid.SetColumn(textBlock, colNum);
    mainGrid.Children.Add(textBlock);  

希望这能回答你的问题

答案 1 :(得分:0)

这项工作非常适合我:

        ListView Lv_Report = new ListView();
        Lv_Report.Name = "Lv_Report";
        Lv_Report.FontSize = 14;
        Lv_Report.Height = 300;
        Lv_Report.Width = 800;
        Canvas.SetTop(Lv_Report, 10);
        Canvas.SetLeft(Lv_Report, 30);
        Cv_Scheduler.Children.Add(Lv_Report);
        Lv_Report.Background = null;


        LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
        myLinearGradientBrush.StartPoint = new System.Windows.Point(0, 0);
        myLinearGradientBrush.EndPoint = new System.Windows.Point(0, 1);
        Color color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#7FFFFFFF");
        myLinearGradientBrush.GradientStops.Add(new GradientStop(color, 0.02));
        color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#BFBADF69");
        myLinearGradientBrush.GradientStops.Add(new GradientStop(color, 0.19));
        color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#7FF9FCF2");
        myLinearGradientBrush.GradientStops.Add(new GradientStop(color, 1));
        color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#BEB9DE67");
        myLinearGradientBrush.GradientStops.Add(new GradientStop(color, 0.83));
        Style style = new Style(typeof(GridViewColumnHeader));
        style.Setters.Add(new Setter()
        {
            Property = GridViewColumnHeader.BackgroundProperty,
            Value = myLinearGradientBrush
        });

        var Lvitems = new List<string>();
            Lvitems.Add("WorkOrder");
            Lvitems.Add("Module1");

        GridView Gv = new GridView();

        foreach (String item in Lvitems)
        {
            DataTemplate Dtemplate = new DataTemplate();
            var markup =

            "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:local=\"clr-namespace:SubMeteringElectric;assembly=SubMeteringElectric\">"
             + "<StackPanel>"
             + "<Label Content =\"{Binding Lv_RSH_" + item + "}\" Width = \"100\" Height = \"30\" HorizontalContentAlignment = \"Center\" FontSize = \"14\" />"
             + "</StackPanel>"
             + "</DataTemplate>";

            byte[] byteArray = Encoding.UTF8.GetBytes(markup);
            MemoryStream stream = new MemoryStream(byteArray);

            Dtemplate = (DataTemplate)XamlReader.Load(stream);

            GridViewColumn Gvc_item = new GridViewColumn();
            Gvc_item.Header = item;
            Gvc_item.Width = 150;

            Gvc_item.CellTemplate = Dtemplate;


            Gv.Columns.Add(Gvc_item);
        }

        Gv.ColumnHeaderContainerStyle = style;
        Lv_Report.View = Gv;
    }