从后面的代码将自定义列表对象属性绑定到文本框 [英] Binding custom list object properties to textbox from code behind

查看:76
本文介绍了从后面的代码将自定义列表对象属性绑定到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学生班

public class Student
{
    string name;        

    List<SubjectInfo> subjectInfoList;        

    ....
}

List<SubjectInfo>列表可以为不同的学生使用不同数量的SubjectInfo对象.

List<SubjectInfo> List can have different number of SubjectInfo objects for different students.

SubjectInfo类

public class SubjectInfo
{
    string subjectCode;

    int marks;

    ...
}

我想在窗口上显示学生对象的详细信息.由于列表具有不同数量的对象计数,因此我从后面的代码生成了这些对象.

I want to display student object detail on a window. Since List have different number of object count I generated these from code behind.

int i = 10;

        foreach (SubjectInfo info in student.SubjectInfoList)
        {
            TextBox tb = new TextBox();
            tb.Width = 200;
            tb.Height = 20;
            tb.Margin = new Thickness(10, i, 0, 0);
            StudentDataGrid.Children.Add(tb);
            i += 60;
        }

我想从后面的代码中绑定此列表列表.但是我不知道这样做.
我想绑定student.SubjectInfoList

I would like to bind this List list from code behind. But I have no idea of doing that.
I want to bind marks property of student.SubjectInfoList

请帮助我从代码隐藏中获取绑定列表对象的属性.

Please help me with binding list object properties from codebehind.

编辑
这是示例学生对象;

EDIT
This is the sample student object;

Student student = new Student("Joe", new List<SubjectInfo>() { new SubjectInfo("Subject1", 50), new SubjectInfo("Subject2", 70)});  

我的窗口应该是这样;

My window should like this;

注意,如果学生总共完成4个科目TextBox的计数为5.

NOTE if student doing 4 subjects total TextBox count is 5.

推荐答案

纯XAML解决方案:

<Window  (... your window attributes) >

    ....

    <Grid x:Name="StudentDataGrid">
        <ListView ItemsSource="{Binding SubjectInfoList}">
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type local:SubjectInfo}">
                    <TextBox Text="{Binding Marks}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

但是要使其正常工作,您必须将字段转换为公共属性:

But to have it work you have to transform your fields to public properties:

public class Student
{
    string name;        

    public List<SubjectInfo> SubjectInfoList { get; set; }

    ....
}

和:

public class SubjectInfo
{
    string subjectCode;

    public int Marks { get; set; }

    ...
}

更新

XAML:

UPDATE

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">


    <Grid x:Name="StudentGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Student name :" Grid.Row="0" Grid.Column="0"/>
        <TextBlock Text="Student marks :" Grid.Row="1" Grid.Column="0"/>

        <TextBox Text="{Binding Name}" Grid.Row="0" Grid.Column="1" />

        <ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding SubjectInfoList}" BorderThickness="0">
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type local:SubjectInfo}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="50"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding SubjectCode}"/>
                        <TextBox Grid.Column="1" Text="{Binding Marks}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

代码:

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Student student = new Student("Joe", new List<SubjectInfo>() { new SubjectInfo("Subject1", 50), new SubjectInfo("Subject2", 70) });
            StudentGrid.DataContext = student;
        }
    }

    public class Student
    {
        public string Name { get; set; }
        public List<SubjectInfo> SubjectInfoList { get; set; }

        public Student(string name, List<SubjectInfo> list)
        {
            Name = name;
            SubjectInfoList = list;
        }
    }

    public class SubjectInfo
    {
        public string SubjectCode { get; set; }
        public int Marks { get; set; }

        public SubjectInfo(string subjectCode, int marks)
        {
            SubjectCode = subjectCode;
            Marks = marks;
        }
    }
}

这篇关于从后面的代码将自定义列表对象属性绑定到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆