0

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

4

2 回答 2

1

我想我误解了你的问题。您的解决方案可能是使用数据网格。

使用 dummydata 创建一些类,您可以将其绑定到 .xaml 文件中的数据网格

您需要知道的一切都应该在本教程中。

http://wpftutorial.net/DataGrid.html

希望它有所帮助。

于 2013-10-18T15:53:41.807 回答
0

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#

定义结构:

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

列表框加载:

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

按钮活动:

private void ActionButtonClick(object sender, RoutedEventArgs e)
  {
       Button btn=sender as Button;
       User user=(User)Button.Tag;   //we gets our user  
      // do something else...
  }
于 2013-10-18T16:11:22.330 回答