【问题标题】:Bind a function to a checkbox in a datagridview in the viewmodel将函数绑定到视图模型中 datagridview 中的复选框
【发布时间】:2021-08-01 13:15:20
【问题描述】:

美好的一天。我在将函数绑定到 datagridview 中的复选框时遇到了一些问题。我正在使用 windows 窗体和 mvvm 模型。我在 datagridview 中有一个数据和一排复选框。如果选中了一个复选框,我希望视图模型使用 if 语句执行一个函数。

这是我的用户界面现在的样子:

例如:如果我选中第一个复选框并按下“创建软件质量报告”按钮,我希望程序在视图模型中执行一个功能。我尝试访问 viewmodel 中的 datagridview,但是使用 mvvm 你不应该从 viewmodel 访问视图。你怎么能做到这一点?视图模型中是否可能有类似:(if datagridview1.checkbox1 == true) {...}?

希望有人能给我一个大致的方向。

提前致谢:)

我想在这个函数内部使用一个函数(我的视图模型中的一个函数):

private void ExecuteCreateSofwareQualityReportButtonClick()
    {
        

        OpenFileDialog OpenExcel = new OpenFileDialog()
        {
            Title = "Save Excel File",
            CheckPathExists = true,
            DefaultExt = "txt",
            Filter = "Excel files (*.xls)|*.xls",
            FilterIndex = 1,
            RestoreDirectory = true
        };

        if (OpenExcel.ShowDialog() == DialogResult.OK)
        {
            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(OpenExcel.FileName);


            //Excel application is stopped and the process is killed
            CloseExcelProcess(xlWorkBook, xlApp);
        }

    }

这是我的模型:

public class ApplicationModel : BindableBase
{
    private Project _selectedProject;

    private Tracker _selectedTracker;

    private string _selectedSoftwareElement;

    public BindingList<Project> ProjectList { get; set; } = new BindingList<Project>();

    public BindingList<Tracker> TrackerList { get; set; } = new BindingList<Tracker>();

    public BindingList<Item> ItemList { get; set; } = new BindingList<Item>();

    public BindingList<string> SoftwareElements = new BindingList<string>();


    public Project SelectedProject
    {
        get 
        { 
            return _selectedProject; 
        }
        set 
        { 
            SetProperty(ref _selectedProject, value);  
        }
    }

    public Tracker SelectedTracker
    {
        get 
        { 
            return _selectedTracker; 
        }
        set
        {
            SetProperty(ref _selectedTracker, value);
        }
    }

    public string SelectedSoftwareElement
    {
        get
        {
            return _selectedSoftwareElement;
        }
        set
        {
            SetProperty(ref _selectedSoftwareElement, value);
        }
    }
}

希望对你有帮助

【问题讨论】:

  • 可以输入部分代码viewmodel和模型代码吗?
  • @MeysamaAsadi 当然。我将编辑我的问题
  • Alternativel 如果有人有的话,我还可以在 Discord 上展示更多内容。我的 Discord 名字是:MartinD#2253
  • 作为复选框绑定到 DataGrid 的变量是什么?
  • 这就是我想要的。我没有变量。我对编码很陌生,并不真正知道 mvvm 中的变量是如何工作的。我浏览了互联网,但只能找到 wpf 的答案。我知道我需要使用 PropertyChanged,但我真的不知道如何

标签: c# winforms mvvm datagridview


【解决方案1】:

我将举一个例子。将您的代码与之匹配。如何在DataGrid中使用checkbox并将其绑定到viewmodel

你的模特

pulic class Model : INotifyPropertyChanged
{
   public int ID { get; set; }
   public string Name { get; set; }
   private bool isCheckBox;

   public bool IsCheckBox
   {
      get { return isCheckBox; }
      set
      {
          isCheckBox = value;
          OnPropertyChange(nameof(IsCheckBox));
      }
   }
   public event PropertyChangedEventHandler PropertyChanged;

   protected void OnPropertyChange(string propertyName)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}

现在ViewModel

public class MainViewModel
{
   public List<Model> myList { get; set; }
   public MainViewModel()
   {
      myList = new List<Model>()
      {
         new Model(){ ID = 1, Name = "name 1" IsCheckBox = true},
         new Model(){ ID = 2, Name = "name 2" IsCheckBox = false},
      };
   }
}

现在在 MainWindow.cs

public MainWindow()
{
   this.DataContext = new MainViewModel();
}

现在MainWindow.xaml

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding myList}">
        <DataGrid.Columns>
           <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
           <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
           <DataGridCheckBoxColumn  Header="Checkbox Column" Binding="{Binding IsCheckBox, Mode=TwoWay}"/> 
        </DataGrid.Columns>
</DataGrid>

【讨论】:

  • 你好,viewmodel和model不应该切换吗?我假设 MainWindow.cs 是视图
  • 感谢您的回答。只是为了清楚我没有使用 WPF
  • 不,它们不应该被改变。每个都是 mvvm 规则的一部分。
猜你喜欢
  • 1970-01-01
  • 2017-05-06
  • 2018-11-29
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多