簡體   English   中英

如何將DataGrid綁定到WPF和MVVM中的動態可變數據和用戶輸入

[英]How to Bind DataGrid to dynamically changeable data and User Inputs in WPF and MVVM

我是WPF的新手,我想在UserControl中顯示如下內容: 示例圖像

我從不同來源獲取數據:

列:

ID:始終存在

問題1 ... n:我有一個像這樣的XML文件

我想從中獲取問題專欄。 問題的數量可以變化。

年級:始終存在

行:

每行僅應包含文本框(某些對用戶而言是只讀的)。

ID列中的數據來自文件。

問題的值由用戶輸入。

將計算成績下的數據。

問題的輸入值和成績的計算值與ID相關。

我嘗試通過使用DataGrid並為問題列設置DataGridTemplateColumn來實現此目的,但並沒有真正成功。 我認為在這種情況下這將是正確的控制,但是我真的無法弄清楚如何進行設置。

我考慮過通過根據數據為“問題”添加標簽和文本框來手動構建此“表”。 但是,然后我的ViewModel應該了解有關違反MVVM的View的一些知識。

謝謝和最好的問候。

編輯:謝謝@Ivan Furdek到目前為止的幫助! 現在,我必須顯示問題的每個文本框和列,並可以對其進行編輯。 但是我的收藏集不會更新-.-所以我的List<double> PointsPerProblems保持初始化值

這是我的XAML:

<ItemsControl ItemsSource="{Binding PointsPerProblems, Mode=TwoWay, 
             UpdateSourceTrigger=PropertyChanged}">                                            
         <ItemsControl.ItemsPanel>
             <ItemsPanelTemplate>
                 <StackPanel Orientation="Horizontal"/>
             </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
         <ItemsControl.ItemTemplate>
             <DataTemplate>
                 <Border Width="70">
                     <TextBox Text="{Binding Path=., Mode=TwoWay,
                      UpdateSourceTrigger=PropertyChanged}"
                      TextAlignment="Center"/>
                 </Border>
             </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我的模特:

public class GradingModel
{
    public string MatriculationNumber { get; set; }

    public List<double> PointsPerProblems { get; set; }

    public double Grade { get; set; }

    public double TotalScore { get; set; }
}

還有我的ViewModel的特定部分:

public ObservableCollection<GradingModel> Gradings
    {
        get
        {
            return this.gradings;
        }

        set
        {
            this.gradings = value;
            this.OnPropertyChanged(nameof(this.Gradings));
        }
    }

public List<string> ProblemList
    {
        get
        {
            return this.problemList;
        }

        set
        {
            this.problemList = value;
            this.OnPropertyChanged(nameof(this.ProblemList));
        }
    }
private void GetGradingForm()
    {
        ExamRatingDTO examRatingModel = new ExamRatingDTO();
        List<StudentDTO> students = new List<StudentDTO>();

        this.messageBoxService.ShowInfoMessage(
            "Please Select a xml-File containing the Exam Ratings.",
            "Select a Exam Rating File.");
        try
        {
           examRatingModel = this.fileDialogService.OpenLoadFileDialog<ExamRatingDTO>();
        }
        catch (InvalidOperationException ex)
        {
            this.messageBoxService.ShowErrorMessage("Please select a correct Exam Rating File.", ex);
        }

        this.messageBoxService.ShowInfoMessage(
            "Please Select a xml-File containing the Students Information.",
            "Select a Student Information File.");
        try
        {
            students = this.fileDialogService.OpenLoadFileDialog<List<StudentDTO>>();
        }
        catch (InvalidOperationException ex)
        {
            this.messageBoxService.ShowErrorMessage("Please select a correct Students File.", ex);
        }

        foreach (var student in students)
        {
            this.Gradings.Add(new GradingModel()
            {
                MatriculationNumber = student.MatriculationNumber.ToString(),
                PointsPerProblems = new List<double>(),
                Grade = 0.0,
                TotalScore = 0.0
            });
        }

        List<string> tmpProblemList = new List<string>();

        foreach (var problem in examRatingModel.PointsPerProblems)
        {
            tmpProblemList.Add(problem.ProblemName);
        }

        foreach (var grading in this.Gradings)
        {
            for (int i = 0; i < tmpProblemList.Count; i++)
            {
                grading.PointsPerProblems.Add(0.0);
            }
        }

        this.ProblemList = tmpProblemList;
    }

編輯

確定在答案的第二次編輯中找到了最后一個問題的解決方案

WPF:如何使帶有動態列的DataGrid綁定可編輯?

您需要將xml解析為一系列對象,例如

    public class Student 
    { 
        public int Id { get; set; } 
        public List<decimal> ProblemScores { get; set; } 
        public DecimalGrade
        { 
            get
            {
               return ProblemScores.Average();
            }
        } 

之后,我建議您按照以下方法獲取所需的顯示: https : //blogs.msmvps.com/deborahk/populating-a-datagrid-with-dynamic-columns-in-a-silverlight-application-using-mvvm/

確保使用雙向綁定,並且使用UpdateSourceTrigger = PropertyChanged以便將更改傳播回列表。

id和score列應將其IsReadOnly屬性設置為true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM