how to create listview to a grid programmatically

2020-08-01 06:43发布

问题:

I have a grid with columns and rows define. By knowing x amount of items, I can add it to my xaml no problem. Unfortuantely I want to create the listview programatically because I want to populate them based on x amount of item I get when executing my SP. Here is my xaml. The label is there to assign a given content from the SP result too. Can someone show me how create this programatically?

<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>

回答1:

we can add rows or columns dynamically from code behind.

Inorder to add rows:

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

Inorder to add colomns:

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

Inorder to add textblock at particular row,column

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

Hope this answers your question



回答2:

This work perfectly for me:

        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;
    }


标签: c# wpf listview