C#关于绑定模型结构的问题

huangapple go评论61阅读模式
英文:

C# Questions about the binding model structure

问题

以下是您提供的代码的中文翻译部分:

*DataModel 类

public DataModel()
{
    this.MeasureA = new BindingList<MiddleMeasure>()
    {
        new MiddleMeasure()
        {
            
        }
    };  
}

public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

*MiddleMeasure 类

public class MiddleMeasure 
{
    public BindingList<MeasureData> MeasureB
    {
        get; set;
    } = new BindingList<MeasureData>();

    public MiddleMeasure()
    {
        MeasureB.Add(new MeasureData()
        {
            Title = "500",
            Title2 = 500,
            Data = 10,
        });

        MeasureB.Add(new MeasureData()
        {
            Title = "501",
            Title2 = 501,
            Data = 20,
        });

        MeasureB.Add(new MeasureData()
        {
            Title = "503",
            Title2 = 503,
            Data = 30,
        });

        MeasureB.Add(new MeasureData()
        {
            Title = "504",
            Title2 = 504,
            Data = 40,
        });
    } 
}

*MeasureData 类

public class MeasureData : INotifyPropertyChanged
{
    private string m_Title;
    private double m_Title2;
    private double m_Data;

    public MeasureData()
    {
        
    }

    public MeasureData(double title, double data)
    {
        this.Title2 = title;
        this.Title = title.ToString();
        this.Data = data;
    }

    public string Title
    {
        get => this.m_Title;
        set 
        {
            this.m_Title = value;
            OnPropertyChanged(nameof(Title));
        }
    }

    public double Title2
    {
        get => this.m_Title2;
        set
        {
            this.m_Title2 = value;
            OnPropertyChanged(nameof(Title2));
        }
    }

    public double Data
    {
        get => this.m_Data;
        set
        {
            this.m_Data = value;
            OnPropertyChanged(nameof(Data));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是您提供的代码的中文翻译。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

*DataModel Class

public DataModel()
            {
                this.MeasureA = new BindingList&lt;MiddleMeasure&gt;()
                {
                    new MiddleMeasure()
                    {
                        
                    }
                };  
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

*MiddleMeasure Class

public class MiddleMeasure 
    {
        public BindingList&lt;MeasureData&gt; MeasureB
        {
            get; set;
        } = new BindingList&lt;MeasureData&gt;();


        public MiddleMeasure()
        {
            MeasureB.Add(new MeasureData()
            {
                Title = &quot;500&quot;,
                Title2 = 500,
                Data = 10,
            });

            MeasureB.Add(new MeasureData()
            {
                Title = &quot;501&quot;,
                Title2 = 501,
                Data = 20,
            });


            MeasureB.Add(new MeasureData()
            {
                Title = &quot;503&quot;,
                Title2 = 503,
                Data = 30,
            });

            MeasureB.Add(new MeasureData()
            {
                Title = &quot;504&quot;,
                Title2 = 504,
                Data = 40,
            });
        } 
    }
  • MeasureData Class

    public class MeasureData : INotifyPropertyChanged
    {
    private string m_Title;
    private double m_Title2;
    private double m_Data;

              public MeasureData()
              {
    
              }
    
              public MeasureData(double title, double data)
              {
                  this.Title2 = title;
                  this.Title = title.ToString();
                  this.Data = data;
              }
    
    
              public string Title
              {
                  get =&gt; this.m_Title;
                  set 
                  {
                      this.m_Title = value;
                      OnPropertyChanged(nameof(Title));
                  }
              }
    
              public double Title2
              {
                  get =&gt; this.m_Title2;
                  set
                  {
                      this.m_Title2 = value;
                      OnPropertyChanged(nameof(Title2));
                  }
              }
    
    
              public double Data
              {
                  get =&gt; this.m_Data;
                  set
                  {
                      this.m_Data = value;
                      OnPropertyChanged(nameof(Data));
                  }
              }
    
    
    
              public event PropertyChangedEventHandler PropertyChanged;
              void OnPropertyChanged(string propertyName)
              {
                  PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
              }
          }
    

It is a structure with MiddleMeasure in DataModel and MeasureData class in it.

The class to bind to the itemsource is the MeasureData class.

this.xDataGrid.ItemsSource = this.m_Model.MeasureA;
If you try this, it won't bind.

Binding like this.m_Model.MeasureA[0].MeasureB is possible, but not what I want. All MeasureB in MeasureA must be bound.

how can i solve it?

答案1

得分: 0

Maybe you misunderstand the binding value in your code.

this.xDataGrid.ItemsSource = this.m_Model.MeasureA; If you try this, it won't bind.

这可能是你的代码中对绑定值的误解。

this.xDataGrid.ItemsSource = this.m_Model.MeasureA; 如果你尝试这样做,它不会绑定。

this.xDataGrid.ItemsSource = this.m_Model.MeasureA: 这一行将ItemSource设置为BindingList&lt;MiddleMeasure&gt;(),它只有一个项(MiddleMeasure)。就像 this.xDataGrid.ItemsSource = BindingList&lt;MiddleMeasure&gt;(){ new MiddleMeasure() } 一样。因此,ItemSource只包含一个项目。并且唯一的项目上只有一个BindingList&lt;MeasureData&gt;属性。

类似 this.m_Model.MeasureA[0].MeasureB 的绑定是可能的,但不是我想要的。必须绑定 MeasureA 中的所有 MeasureB。

this.m_Model.MeasureA[0].MeasureB 的值是 BindingList&lt;MeasureData&gt;。所以,当你将其设置为ItemSource时,绑定成功。

绑定到ItemSource的类是 MeasureData 类

这样,this.xDataGrid.ItemsSource 的值必须是 BindingList&lt;MeasureData&gt;。但是你说必须绑定 MeasureA 中的所有 MeasureB。因此,你需要声明一个变量(类型为BindingList&lt;MeasureData&gt;)来获取 MeasureA 的每个项的 MeasureB。

如果是这样的话,MiddleMeasure 类就没有意义了。你可以让 MeasureA 的类型为 BindingList&lt;MeasureData&gt;

英文:

Maybe you misunderstand the binding value in your code.

> this.xDataGrid.ItemsSource = this.m_Model.MeasureA; If you try this, it won't bind.

this.xDataGrid.ItemsSource = this.m_Model.MeasureA: this line set the itemsource as the BindingList&lt;MiddleMeasure&gt;() which just have one item(MiddleMeasure) in it. Just like the this.xDataGrid.ItemsSource = BindingList&lt;MiddleMeasure&gt;(){ new MiddleMeasure() }. So the ItemSource just have one item. And there is only a BindingList&lt;MeasureData&gt; property on the only item.

> Binding like this.m_Model.MeasureA[0].MeasureB is possible, but not what I want. All MeasureB in MeasureA must be bound

The value of the this.m_Model.MeasureA[0].MeasureB is BindingList&lt;MeasureData&gt;. So you bind successfully when you set it as the itemsource.

> The class to bind to the itemsource is the MeasureData class

The the value of the this.xDataGrid.ItemsSource must be BindingList&lt;MeasureData&gt;. But you said all MeasureB in MeasureA must be bound. So you need to declare a variable( type of BindingList&lt;MeasureData&gt; ) to get MeasureA's every item's MeasureB.

If so, the MiddleMeasure class has no meanings.You can just let the MeasureA be type of BindingList&lt;MeasureData&gt;.

huangapple
  • 本文由 发表于 2023年3月7日 09:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定