none
wpf,ListView如何绑定Tuple类型的泛型数据集合? RRS feed

  • 问题

  •  

    有一个需求,把两个实体类型不同的泛型集合的某些属性,通过tuple数据类型的泛型集合组合在一起(类似数据库中两个结构不同的数据表的多表查询)。然后怎么与listview绑定。listview的数据模板要怎么写?

    不想多创建一个类来封装,创建该类型的泛型集合进行绑定




    2017年5月26日 10:36

答案

  • Hi,

    >>有一个需求,把两个实体类型不同的泛型集合的某些属性,通过tuple数据类型的泛型集合组合在一起(类似数据库中两个结构不同的数据表的多表查询)。然后怎么与listview绑定。listview的数据模板要怎么写?

    使用Tuple类型绑定listview,数据模板中可以直接使用Item1...来访问。

     public partial class TupleBinding: Window
        {
            static string connectString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyDataBase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            public TupleBinding()
            {
                InitializeComponent();
                myLV.ItemsSource = GetStudentView();
            }
            public List<Tuple<int, string, int, string, int>> GetStudentView()
            {
                string sql = @"select Student.ID,Student.Name,Student.Age,Teacher.Name,Teacher.Age from Student,Teacher where Student.TeacherlId=Teacher.Id";
    
                List<Tuple<int, string, int, string, int>> list = new List<Tuple<int, string, int, string, int>>();
    
                using (SqlConnection sqlConn = new SqlConnection(connectString))
                {
                    sqlConn.Open();
                    using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
                    {
                        using (SqlDataAdapter sqlAda = new SqlDataAdapter(sqlCmd))
                        {
                            DataSet ds = new DataSet();
                            sqlAda.Fill(ds);
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                Tuple<int, string, int, string, int> t = new Tuple<int, string, int, string, int>(Convert.ToInt32(row[0]), Convert.ToString(row[1]), Convert.ToInt32(row[2]), Convert.ToString(row[3]), Convert.ToInt32(row[4]));
                                list.Add(t);
                            }
                        }
                    }
                }
                return list;
            }
        }
      <Grid>
            <ListView x:Name="myLV">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Border  BorderBrush="Red" BorderThickness="2">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Background="AliceBlue" Margin="10,0,10,10" Grid.Column="0" Text="{Binding Item1}"/>
                                <TextBlock Background="AntiqueWhite" Margin="10,0,10,10" Grid.Column="1" Text="{Binding Item2}"/>
                                <TextBlock Background="Aqua" Margin="10,0,10,10" Grid.Column="2" Text="{Binding Item3}"/>
                                <TextBlock Background="Aquamarine" Margin="10,0,10,10" Grid.Column="3" Text="{Binding Item4}"/>
                                <TextBlock Background="Azure" Margin="10,0,10,10" Grid.Column="4" Text="{Binding Item5}"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年5月29日 9:36
    版主