简体   繁体   中英

How to show some dummy data in ListView

I am new to C# and I am developing a prototype for a software. I want to create a table like structure:

Name                  Details        ActionButton
-------------------------------------------------
Name1                 Details1       Button
Name1                 Details1       Button
Name1                 Details1       Button

So, I want to fill the rows of this table from dummy data. Whatever I found till now, uses some code to bind the listview to some data source. However, I just want to know is this possible to add data directly into XAML

I think I misunderstood your question. A solution in your case could be to use a datagrid.

Make some class with dummydata, which you could bind to the datagrid in the .xaml file

Everything you need to know, should be in this tutorial.

http://wpftutorial.net/DataGrid.html

Hope it helped.

XAML:

<!-- HEADER-->
   <StackPanel Orientation="Horizontal" >
                <TextBlock Width="250" TextAlignment="Center" Text="User"/>
                <TextBlock Width="250" TextAlignment="Center" Text="Details"/>
                <TextBlock Width="100" TextAlignment="Center" Text="Action"/>
            </StackPanel>

 <!--ListView-->
    <ListView x:Name="lb_Users">
      <ListView.ItemTemplate>
        <DataTemplate>
           <Grid Width="600">
              <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="250"/>
                 <ColumnDefinition Width="250"/>
                 <ColumnDefinition Width="100"/>
              </Grid.ColumnDefinitions>
             <TextBlock  Text="{Binding Name}" />
             <TextBlock  Text="{Binding Details}" />
             <Button Tag="{Binding}" Click="ActionButtonClick"/>
            </Grid>
         </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

C#

Define struct:

struct User
    {
        public string Name { get; set; }
        public string Details { get; set; }
    }

ListBox Load:

public void ShowList()
{
        List<User>Users=new List<User>();
        //Here we have to fill this list by some data.
        lb_Users.ItemSource=Users;
}

Button activity:

private void ActionButtonClick(object sender, RoutedEventArgs e)
  {
       Button btn=sender as Button;
       User user=(User)Button.Tag;   //we gets our user  
      // do something else...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM