【问题标题】:Binding custom list object properties to textbox from code behind从后面的代码将自定义列表对象属性绑定到文本框
【发布时间】:2013-01-29 17:57:49
【问题描述】:

学生班

public class Student
{
    string name;        

    List<SubjectInfo> subjectInfoList;        

    ....
}

List&lt;SubjectInfo&gt; 列表对于不同的学生可以有不同数量的SubjectInfo 对象。

SubjectInfo 类

public class SubjectInfo
{
    string subjectCode;

    int marks;

    ...
}

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

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;
        }

我想从后面的代码中绑定这个 List 列表。但我不知道这样做。
我想绑定student.SubjectInfoListma​​rks属性

请帮助我从代码隐藏中绑定列表对象属性。

编辑
这是示例学生对象;

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

我的窗口应该是这样的;

注意如果学生总共完成 4 门科目TextBox 计数为 5。

【问题讨论】:

  • 你在用什么,WPF 还是 Winforms?
  • WPF 和我想从后面的代码进行绑定。
  • 你为什么希望它来自后面的代码?您绑定到哪些控件?
  • 因为 List&lt;SubjectInfo&gt; 计数对于不同的学生可以有不同的计数。窗口将显示学生信息。我想将 marks 属性绑定到文本框。例如,如果给定学生做 5 个科目 List&lt;SubjectInfo&gt; 计数为 5。我想生成 5 个文本框并在该文本框上显示标记
  • 那么你可以在 XAML 中完成所有操作。

标签: c# wpf binding


【解决方案1】:

纯 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>

但要让它工作,你必须将你的字段转换为公共属性:

public class Student
{
    string name;        

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

    ....
}

和:

public class SubjectInfo
{
    string subjectCode;

    public int Marks { get; set; }

    ...
}

更新

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;
        }
    }
}

【讨论】:

  • 为什么我没有选择ListView,因为这个窗口就像一个表格。 :(
  • @NewDeveloper 我已经更新了我的帖子。如果你有任何问题可以问我。但现在我得回家了;)。
  • 好的。谢谢。如果我有问题,我肯定会问问题。 :)
【解决方案2】:

最好在 XAML 中进行。 但是,如果您真的想在代码隐藏中执行此操作,请尝试此操作。 SubjectInfo 的字段必须是公开的。

foreach (SubjectInfo info in student.SubjectInfoList)
{
   TextBox tb = new TextBox();
   ///...
   tb.DataContext = info;
   Binding binding = new Binding("marks");
   tb.SetBinding(TextBox.TextProperty, binding);
   ///...
}

【讨论】:

  • @NewDeveloper 是个坏主意,但还可以。它解决了您的问题,但它不是处理 WPF 和数据绑定的正确方法。
猜你喜欢
  • 2019-08-07
  • 1970-01-01
  • 2017-01-01
  • 2016-01-16
  • 2018-11-16
  • 1970-01-01
  • 2016-08-25
  • 2016-04-02
  • 2018-07-02
相关资源
最近更新 更多