【问题标题】:Trying to move data binding from MainWindow.xaml.cs to another ViewModel module尝试将数据绑定从 MainWindow.xaml.cs 移动到另一个 ViewModel 模块
【发布时间】:2018-10-09 14:53:31
【问题描述】:

我正在尝试创建一个从串行端口到达的值表。新数据到达串行端口后,表格将立即更新。

目前只有一个 xaml 文件。

我一直在关注this 实现,该实现到目前为止有效,但仅当我将对象数组分配给 MainWindow.xaml.cs 中的 DataGrid(如该示例中所示)时,它曾经看起来像这样:

public MainWindow()
       {
           InitializeComponent();             

            var TableDat = new ObservableCollection<LineViewModel>()
            {
            new LineViewModel(1,2,888,6,5), // Random values to see if anyhting dipslays
            new LineViewModel(122,2,888,6,5),
            };        

            this.dataGrid1.ItemsSource = TableDat ;
    }

XAML 中的 DataGrid 如下所示:

  <DataGrid AutoGenerateColumns="False" 
        Height="Auto" 
        HorizontalAlignment="Left" 
        Name="dataGrid1" 
        VerticalAlignment="Top" 
        ScrollViewer.CanContentScroll="True" 
        ScrollViewer.VerticalScrollBarVisibility="Visible"
        Grid.Row="1">

        <DataGrid.Columns >
            <DataGridTextColumn Binding="Item1" Width="*">
            <DataGridTextColumn Binding="Item2" Width="*" />
            <DataGridTextColumn Binding="Item3" Width="*" />
            <DataGridTextColumn Binding="Item4" Width="*" />
            <DataGridTextColumn Binding="Item5" Width="*" />
        </DataGrid.Columns>
    </DataGrid>

我想将我的项目划分为我认为正确的 MVVM 布局,其中包含模型、视图模型、视图文件的单独文件夹,而不是主类中发生的所有内容。

我希望整个结构大致类似于
View ViewModel Model

为此,我创建了另一个文件 DataGridViewModel.cs,它将实例化对象数组并将其传递给 dataGrid1 DataGrid。

这是我的 MainWindow.xaml.cs

 public partial class MainWindow : Window {           

        private DataGridViewModel _dat = new DataGridViewModel();               

        public MainWindow()
        {     
           InitializeComponent();
           DataContext = _dat;
        }
}

这是我想将对象数组传递给提到的 DataGrid 的类。

class DataGridViewModel : ObservableObject
    {            
        public ObservableCollection<LineViewModel> TableDat { get; private set; }

        public DataGridViewModel()
        {
            var TableDat = new ObservableCollection<LineViewModel>()
            {                    
                new LineViewModel(1,2,888,6,5),
                new LineViewModel(122,2,888,6,5),
            };

            //Here I would like to pass the object array to the data grid, dataGrid1
        }           
    }

现在

this.dataGrid1.ItemsSource = TableDat;

产量:

Severity Code Description Project File Line Suppression State Error CS1061
'DataGridViewModel' does not contain a definition for 'dataGrid1' and no accessible
 extension method 'dataGrid1' accepting a first argument of type 'DataGridViewModel' 
could be found(are you missing a using directive or an assembly reference ?)

我猜这是因为我试图将它传递给 MainWindow.xaml 而不是 DataGrid.xaml。

如果我将 this 更改为 MainWindow 并尝试使用

MainWindow.dataGrid1.ItemsSource = TableDat;

我希望它将它链接到我得到的适当的 xaml 文件:

Severity Code Description Project File Line Suppression State Error CS0120
An object reference is required for the non-static field, method, or property 
'MainWindow.dataGrid1'

我是面向对象的新手,目前其中许多概念都超出了我的想象,所以如果我没有准确地表达我想说的话,也没有使用正确的术语,请原谅我。

我正处于失败的境地并接受如果我设法从我的 MainWindow.xaml.cs 正确传递数组,我还不如在那里做其他所有事情,而不是多花几天时间(周)试图弄清楚如何正确或完全做到这一点。

谢谢

【问题讨论】:

  • 在后面的代码中分配 ItemsSource 属性不是数据绑定。用 XAML 编写:&lt;DataGrid ItemsSource="{Binding TableDat}" .../&gt;。 Binding 在当前 DataContext 中查找 TableDat 属性,即在您分配给它的 _dat 对象中。完成。
  • 另请阅读Data Binding Overview
  • 这是数据绑定,至少对于单元格而言。

标签: c# wpf xaml mvvm data-binding


【解决方案1】:

您应该设置视图模型的TableDat 属性。为此,您只需从构造函数中删除 var 关键字:

class DataGridViewModel : ObservableObject
{
    public ObservableCollection<LineViewModel> TableDat { get; private set; }

    public DataGridViewModel()
    {
        TableDat = new ObservableCollection<LineViewModel>()
        {
            new LineViewModel(1,2,888,6,5),
            new LineViewModel(122,2,888,6,5),
        };
    }
}

然后您可以使用{Binding} 语法将DataGridItemsSource 属性绑定到返回ObservableCollection&lt;LineViewModel&gt; 的视图模型的TableDat 属性:

<DataGrid ItemsSource="{Binding TableDat}" ... />

【讨论】:

  • 很好看。删除微小的var。请注意,该属性也可以是只读的:public ObservableCollection&lt;LineViewModel&gt; TableDat { get; }。如果不是,它应该触发属性更改通知。
  • 你是个传奇mm8。它不知道如何工作,但希望它会及时到来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2011-05-19
  • 1970-01-01
  • 2015-04-27
相关资源
最近更新 更多