【问题标题】:How to make use of the information the user added in a list from DataContext?如何利用用户在 DataContext 列表中添加的信息?
【发布时间】:2019-03-25 23:22:27
【问题描述】:

使用来自主网格的数据上下文,我尝试使用从用户那里收集的信息并将其存储在一个列表中,然后使用该列表并能够通过 LINQ 获取信息。

ColDiv._inventaire.Add(inventaire);

Coldiv 是我的 cs 文件之一,_inventaire 是列表,inventaire 是数据上下文中的数据。

ColDiv 的代码在我的班级之一中:

class CollectionsDiverses
{
    public List<Client> _client = new List<Client>();
    public List<Inventaire> _inventaire = new List<Inventaire>();
    public List<Vente> _VenteArticle = new List<Vente>();

这是将网格中的数据上下文放入属性和列表中的代码。 (如下所列)

Inventaire inventaire = null;
frmArticle frmArticle = new frmArticle

bool? bReturn = frmArticle.ShowDialog();
if (bReturn == true)
{
    inventaire = (Inventaire)frmArticle.grdMain.DataContext;
    inventaire.Créé = DateTime.Now.ToString();
    ColDiv._inventaire.Add(inventaire);
    dgInventaire.Items.Refresh()
}

现在从逻辑上讲,它已被存储。现在,来自另一个带有 TextChanged 事件的 TextBox,捕获(例如)客户端的 ID:

private void TxtNoArticle_TextChanged(object sender, TextChangedEventArgs e)
{
    try
    {
        _venteEdition.NoArticle = Int32.Parse(txtNoArticle.Text); 
        _venteEdition.ArticleComplet = ArticleCompletToString(_venteEdition.NoArticle);
        lblArticleComplet.Content = _venteEdition.ArticleComplet;
    }
    catch (Exception){}

然后从一个方法中继续使用 LINQ 以查找具有用户输入的 ID 的客户端:

public string ArticleCompletToString(int iNombre)
{
    string sArticle = "";

    var req = from art in ColDiv._inventaire
              where art.No == iNombre // art.No is the ID the user wants and iNombre is the ID the user entered in the TextBox
              select new
              {
                  art.Modèle,
                  art.Marque
              };
    foreach (var i in req)
    {
        sArticle = i.Modèle + " " + i.Marque;
    }
    return sArticle;

它应该从列表中准确返回我想要的信息,但看起来它甚至没有创建新的 Inventaire。

【问题讨论】:

  • 你如何定义ColDiv_ColDiv...?请阅读this 并相应修改您的问题。
  • 他们是一样的
  • 那么如何初始化ColDiv
  • 我在公共部分类 MinaWindow 的正下方初始化:Window ... 我这样初始化它:private CollectionsDiverses ColDiv = new CollectionsDiverses();

标签: c# wpf list linq


【解决方案1】:

好的,找到了问题。我以三种不同的形式初始化 ColDiv 三次 因此,您唯一需要做的就是在主体中进行初始化并使其公开和静态。在另一种形式中初始化它只需执行以下操作:

public partial class frmVentes : Window
{
    private CollectionsDiverses _ColDiv;

    public frmVentes()
    {
        InitializeComponent();
        this._ColDiv = MainWindow.ColDiv;
    }

//...

在您的主代码中,它看起来像这样:

public partial class MainWindow : Window
{
    public static CollectionDiverses ColDiv = new CollectionsDiverses();


    public MainWindow()
    {
        InitializeComponent();
    }
//...

现在在您的 CollectionsDiverses 类中,您想像这样将其更改为公开

public class CollectionsDiverses
{
//...

您必须为您的列表创建一个内部属性:

private List<Client> client = new List<Client>();
private List<Inventaire> inventaire = new List<Inventaire>();
//...

internal List<Client> Client { get => client; set => client = value; }
internal List<Vente> VenteArticle { get => venteArticle; set => venteArticle = value; }
//...

就是这样!现在已经修复了,我可以使用不同表单之间的信息了!

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多