2013-01-18 106 views
0

我在WPFToolkit:DataGrid中用数据绑定做了一些应用程序。 当我运行应用程序时,我抓住这个错误,例如:BindingExpression路径错误:在'对象'上找不到'Data'属性

System.Windows.Data Error: 40 : BindingExpression path error: 'ProcHandle' property not found on 'object' ''ProcInfo' (HashCode=61374531)'. BindingExpression:Path=ProcHandle; DataItem='ProcInfo' (HashCode=61374531); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

在MainWindow.xaml:

<toolkit:DataGrid x:Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="0,47,0,0"> 
      <toolkit:DataGrid.Columns> 
       <toolkit:DataGridTextColumn Header="Time/sec" Binding="{Binding KillTime}"/> 
       <toolkit:DataGridTextColumn Header="Handle" Binding="{Binding ProcHandle}"/> 
       <toolkit:DataGridTextColumn Header="Start Time" Binding="{Binding StartTime}"/> 
       <toolkit:DataGridTextColumn Header="Status" Binding="{Binding ProcStatus}"/> 
       <toolkit:DataGridTextColumn Header="Priority" Binding="{Binding ProcPriority}"/> 
       <toolkit:DataGridTextColumn Header="End Time" Binding="{Binding EndTime}"/> 
      </toolkit:DataGrid.Columns> 
     </toolkit:DataGrid> 

在MainWindow.xaml.cs

public partial class MainWindow : Window 
    { 
     private Process _firstProc; 

     private DispatcherTimer _timerFirstProc; 

     ProcessCollectionClass _procCollection = new ProcessCollectionClass(); 

     private int _firstProcTime; 

public MainWindow() 
     { 
      InitializeComponent(); 
      DG1.DataContext = _procCollection.ProcCollection; 
     } 

     internal class ProcessCollectionClass : INotifyPropertyChanged 
     { 
      private ObservableCollection<ProcInfo> _procCollection = new ObservableCollection<ProcInfo>(); 

      public event PropertyChangedEventHandler PropertyChanged; 

      public ObservableCollection<ProcInfo> ProcCollection 
      { 
       get { return _procCollection; } 
       set 
       { 
        _procCollection = value; 
        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, new PropertyChangedEventArgs("ProcCollection")); 
        } 
       } 
      } 
     } 

     public class ProcInfo 
     { 
      public ProcInfo(string killTime, string procHandle, string startTime, 
          string procStatus, string procPriority) 
      { 
       KillTime = killTime; 
       ProcHandle = procHandle; 
       StartTime = startTime; 
       ProcStatus = procStatus; 
       ProcPriority = procPriority; 
      } 

      private string KillTime { get; set; } 
      private string ProcHandle { get; set; } 
      private string StartTime { get; set; } 
      private string ProcStatus { get; set; } 
      private string ProcPriority { get; set; } 
     } 

private void FirstProc_ButClick(object sender, RoutedEventArgs e) 
     { 

      _firstProcTime = Int32.Parse(FirstProcTime.Text); 
      _firstProc = new Process(); 
      _firstProc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe"; 
      _firstProc.StartInfo.UseShellExecute = true; 
      _firstProc.Start(); 
      _firstProc.PriorityClass = ProcessPriorityClass.High; 

      _timerFirstProc = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) }; 
      _timerFirstProc.Start(); 
      _timerFirstProc.Tick += _timerFirstProc_Tick; 
} 
void _timerFirstProc_Tick(object sender, EventArgs e) 
     { 
      --_firstProcTime; 

      _firstProc.Refresh(); 
      _procCollection.ProcCollection.Add(new ProcInfo(
                _firstProcTime.ToString(), 
                _firstProc.Handle.ToString(), 
                _firstProc.StartTime.ToString(), 
                _firstProc.Responding ? "Running" : "Not Responding", 
                _firstProc.PriorityClass.ToString())); 

      if (_firstProcTime == 0) 
      { 
       _firstProc.Kill(); 
       _timerFirstProc.Stop(); 
      } 
} 

在结束我在DataGrid中只看到空行。我究竟做错了什么!? 对不起我的英文不好

回答

5

您在ProcInfo这个课程中的所有属性都是私有的。他们应该是public。或者至少有一个公共的getter。

+0

非常感谢!从外面看是总是有用和酷=) –

+1

想到我会补充说,他们不仅必须**公共**属性,但他们也必须是公共**属性**。这个答案最终导致我意识到,我试图绑定到公共领域,谢谢。 – MatrixManAtYrService

相关问题