2017-01-09 68 views
0

我试图进入更好的编程习惯,所以我一直在通过各种MVVM示例工作。使用MVVM在WPF中查询后更新Datagrid

在这个应用程序,我有一个看法。我希望这从用户从组合框中选择的选项(现在这是硬编码),并在运行SQL查询后用选定结果更新数据网格。

到目前为止,我能拿到第一批成果在创建ResultViewModelusing LoadResults();)的,但是当我运行下一个SQL查询(using OnUpdate();)的datagrid不与结果更新(我已经签了查询通过调试运行良好)。

我想我从一个教程到另一个教程时错过了一些东西。我有一种感觉,我可能需要在ResultViewModel上使用INotifyPropertyChanged,但我尝试过实施它,但无法使其正常工作。

任何意见将不胜感激。谢谢!

ResultModel

public class ResultModel { } 

public class Result : INotifyPropertyChanged 
{ 
    private string theHomeTeam; 
    private string theVisitorTeam; 


    public string HomeTeam 
    { 
     get { return theHomeTeam; } 

     set 
     { 
      if (theHomeTeam != value) 
      { 
       theHomeTeam = value; 
       RaisePropertyChanged("HomeTeam"); 
      } 
     } 
    } 

    public string VisitorTeam 
    { 
     get { return theVisitorTeam; } 

     set 
     { 
      if (theVisitorTeam != value) 
      { 
       theVisitorTeam = value; 
       RaisePropertyChanged("VisitorTeam"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

} 

ResultViewModel.cs

公共类ResultViewModel {

public ObservableCollection<Result> Results { get; set; } 

    private Result _selectedResult; 

    public MyICommand DeleteCommand { get; set; } 
    public MyICommand SaveCommand { get; set; } 
    public MyICommand UpdateCommand { get; set; } 

    public ResultViewModel() 
    { 
     LoadResults("Liverpool", "Everton"); 
     DeleteCommand = new MyICommand(OnDelete, CanDelete); 
     SaveCommand = new MyICommand(OnSave, CanSave); 
     UpdateCommand = new MyICommand(OnUpdate, CanUpdate); 
    } 


    public void LoadResults(string theHomeTeam, string theVisitorTeam) 
    { 
     ObservableCollection<Result> results = new ObservableCollection<Result>(); 

     string connectionString = ConfigurationManager.ConnectionStrings["AzureCon"].ConnectionString; 
     string CmdString = string.Empty; 

     using (SqlConnection con = new SqlConnection(connectionString)) 
     { 
      CmdString = "SELECT * FROM tblEnglishLeagueResults WHERE elrHomeTeam = '" + theHomeTeam + "' AND elrVisitorTeam = '" + theVisitorTeam + "';"; 
      SqlCommand cmd = new SqlCommand(CmdString, con); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable("Results"); 
      sda.Fill(dt); 

      foreach (DataRow row in dt.Rows) 
      { 
       results.Add(new Result { HomeTeam = Convert.ToString(row[3]), VisitorTeam = Convert.ToString(row[4]) }); 
      } 

     } 
     Results = results; 
    } 

    public Result SelectedResults 
    { 
     get { return _selectedResult; } 
     set 
     { 
      _selectedResult = value; 
      DeleteCommand.RaiseCanExecuteChanged(); 
      SaveCommand.RaiseCanExecuteChanged(); 
     } 
    } 

    private bool CanDelete() { return SelectedResults != null; } 

    private void OnDelete() { Results.Remove(SelectedResults); } 

    private bool CanUpdate() { return true; } 

    private void OnUpdate() { LoadResults("Liverpool", "Sunderland"); } 

    private bool CanSave() { return true; } 

    private void OnSave() { //SAVE LOGIC} 

}

ResultView.xaml

<DataGrid x:Name="grdResults" AutoGenerateColumns="False" ItemsSource="{Binding Results, Mode=TwoWay}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=HomeTeam}" Header="Home Team"/> 
      <DataGridTextColumn Binding="{Binding Path=VisitorTeam}" Header="Visitor Team"/> 
     </DataGrid.Columns> 
    </DataGrid> 

回答

0

尝试从INotifyPropertyChanged的继承您的视图模型

public class ResultViewModel : INotifyPropertyChanged 

和通知添加到视图列表:

private ObservableCollection<Result> _Results; 

public ObservableCollection<Result> Results 
{ 
    get { return this._Results; } 

    set 
    { 
     this._Results= value; 
     RaisePropertyChanged("Results");   
    } 
} 
+0

就这样它作品。非常感谢你!你已经为我节省了很多时间,试图在互联网的深处找到答案。 – AndyC

+0

乐意提供帮助,变更在您的结果中,结果属性在您ViewModel中,如果您没有通知它,视图如何知道变更?答案是在您的VM中INotifyPropertyChanged。 – MyB

+0

有道理。在MVVM旅程中学到了一个好教训,又迈出了一步。再次感谢。 – AndyC