【发布时间】:2015-05-20 12:33:03
【问题描述】:
我正在使用 MVVM 构建一个基本的 WPF 应用程序。我正在尝试获取有关不同发电厂的数据以显示在 Datagrid 上。该程序构建良好并显示网格线,但没有数据。我看过很多类似的问题并尝试了几件事,但似乎没有任何效果。我想知道这个问题是否只是我不知道要寻找的简单事情。
查看:
public class Plant : INotifyPropertyChanged
{
#region Properties
private int plantID;
public int PlantID
{
get { return plantID; }
set
{
plantID = value;
RaisePropertyChanged("PlantID");
}
}
//...etc for all properties//
#endregion
void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
视图模型:
public class MainViewModel : ViewModelBase, IPlantViewModel
{
private ObservableCollection<Plant> _plants = new ObservableCollection<Plant>();
private ICommand _loadCommand;
public MainViewModel()
{
_plants = GetPlants();
_plants.Add(new Plant (){PlantID=1, Name="Orange Plant", Address="1111 Orange Road", PlantPhone="(314)456 7489",PlantFax="(593)202 6948", Contact="John Smith"});
_plants.Add(new Plant() { PlantID = 2, Name = "Blue Plant", Address = "2222 Blue Lane", PlantPhone = "6368967483", PlantFax = "783279948", Contact = "Jane Doe" });
}
public ObservableCollection<Plant> Plants
{
get { return _plants; }
}
public ObservableCollection<Plant> GetPlants()
{
if (_plants == null || _plants.Count == 0)
_loadCommand.Execute(_plants);
return _plants;
}
}
查看:
<UserControl
x:Class="Directory.MVVM.View.DirectoryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:Directory.ViewModel"
Height="481" Width="708">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="21*"/>
<ColumnDefinition Width="79*"/>
</Grid.ColumnDefinitions>
<DataGrid
ItemsSource="{Binding Source=Plants}"
Name="PlantGrid"
HorizontalAlignment="Left"
Margin="82,54,0,0"
VerticalAlignment="Top"
Height="147" Width="515"
AutoGenerateColumns="False" Grid.ColumnSpan="2">
<DataGrid.DataContext>
<local:MainViewModel />
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="Plant ID" Width="89" Binding="{Binding Path=PlantID,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<DataGridTextColumn Header="Name" Width="140" Binding="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay }" />
<DataGridTextColumn Header="Address" Width="90" Binding="{Binding Path=Address, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
<DataGridTextColumn Header="Phone" Width="90" Binding="{Binding Path=PlantPhone, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<DataGridTextColumn Header="Fax" Width="60" Binding="{Binding Path=PlantFax,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
<DataGridCheckBoxColumn Header="Contact" Width="58" Binding="{Binding Path=Contact, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
视图背后的代码(不确定我是否应该在此处添加更多内容):
public partial class DirectoryView : UserControl
public DirectoryView()
{
InitializeComponent();
}
另外,我以此作为参考:
http://wpfgrid.blogspot.com/2014/02/simple-observablecollection-wpf-mvvm.html
【问题讨论】:
-
你没有遗漏一些代码吗? Plant 对象只有 PlantID
-
我还有几个属性,但我只包含了一个来解决问题。关键是,所有的属性都是这样设置的。
-
我还没有看到任何问题。
-
如果在代码隐藏中设置数据上下文是否有效?像这样。DataContext=new MainViewModel()
-
为什么这被否决了,好像 MVVM 很简单。
标签: c# wpf mvvm datagrid observablecollection