Home > Software design >  WPF How I should add list data in Grid
WPF How I should add list data in Grid

Time:12-06

 <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        
    </Grid>

I would like to add the TextBlock from the list here

enter image description here

ViewModel Code enter image description here

I try to use listbox But that's not what I want

<ListBox ItemsSource="{Binding Serials}" Grid.Row="1">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding aaa}" Margin="20"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

enter image description here

I'll frame the grid,And make pagination What I want is TextBlock in the grid center.

How should I do it?

CodePudding user response:

The approach here is wrong, instead of grid you can simply use ListView, it will dynamically use the space and display in grid view.

We populate a list of our own User objects, each user having a name and an age. The data binding process happens automatically as soon as we assign the list to the ItemsSource property of the ListView, but the result is a bit discouraging:

Here is the sample code :

Xaml :

<Grid>
  <ListView Margin="10" Name="lvDataBinding"></ListView>
</Grid>

c#

public partial class ListViewDataBindingSample : Window
 {
    public ListViewDataBindingSample()
    {
        InitializeComponent();
        List<User> items = new List<User>();
        items.Add(new User() { Name = "John Doe", Age = 42 });
        items.Add(new User() { Name = "Jane Doe", Age = 39 });
        items.Add(new User() { Name = "Sammy Doe", Age = 13 });
        lvDataBinding.ItemsSource = items;
    }
 }

public class User
{
    public string Name { get; set; }

    public int Age { get; set; }
}

CodePudding user response:

I think you are confused between Grid and Listview.

Why do you want to use both?

Try a Horizontal Listview.

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"></StackPanel>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

CodePudding user response:

XAML code

<Grid Grid.Row="1" x:Name="gridMain">
    <Grid.ColumnDefinitions>
...
</Grid>

Code behind

TextBlock textBlock = new TextBlock();
textBlock.Text = "I'm here!";

Grid.SetRow(textBlock, 2);
Grid.SetColumn(textBlock, 2);
gridMain.Children.Add(textBlock);
  • Related