2013-10-15 142 views
1

我有一个WPF窗口与一些文本框和一个DataGrid。 DataGrid中充满了数据,但我需要它,当用户单击该数据网格中的单元格时,程序将检测该行并用该行中的数据重新填充文本框。 例如,有一个ID,Name和BirthDate文本框。当用户单击给定行中的任何单元格时,文本框ID,Name和BirthDate的值必须成为所选行中各列(ID,Name,BirthDate)的值。 我环顾四周寻找答案,但我似乎只能找到与WinForms相关的答案,并且WinForms和WPF中的DataGrid工作方式完全不同,代码明智。C# - 从WPF DataGrid填充文本框

+0

[教程](http://www.codeproject.com/Articles/332615/WPF-Master-Details-MVVM-Application)。你的问题对于Stackoverflow来说太宽泛了。顺便说一句winforms糟透了。 –

+2

我知道它很烂,这就是为什么我使用WPF。我会考虑你给我的那个链接。 – chaosflaw

+0

需要了解有关MVVM模式。 MVVM将“经常”使WPF更轻松。 – HichemSeeSharp

回答

5

您可以简单地使用Binding使用ElementName属性来为你做这个......没有后面的代码需要:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <DataGrid Name="DataGrid" ItemsSource="{Binding YourDataCollection}" /> 
    <Grid Grid.Column="1"> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Text="{Binding SelectedItem.Id, 
      ElementName=DataGrid}" /> 
     <TextBlock Grid.Row="1" Text="{Binding SelectedItem.Name, 
      ElementName=DataGrid}" /> 
     <TextBlock Grid.Row="2" Text="{Binding SelectedItem.BirthDate, 
      ElementName=DataGrid}" /> 
    </Grid> 
</Grid> 
+0

似乎工作得很好,非常感谢! – chaosflaw

0

这里我做了一个小小的例证按照你原来的例子:

//XAML 
    <Grid> 
      <DataGrid ItemsSource="{Binding Persons}" Margin="0,0,136,0" SelectedItem="{Binding SelectedPerson}"></DataGrid> 
      <Label Content="{Binding SelectedPerson.Id}" HorizontalAlignment="Left" Margin="400,35,0,0" VerticalAlignment="Top" Width="90" Height="26"/> 
      <Label Content="{Binding SelectedPerson.Name}" HorizontalAlignment="Left" Margin="400,97,0,0" VerticalAlignment="Top" Width="90" Height="24"/> 
      <Label Content="{Binding SelectedPerson.BirthDate}" HorizontalAlignment="Left" Margin="400,66,0,0" VerticalAlignment="Top" Width="90" Height="26"/> 
     </Grid> 
//.cs 
    public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = new PersonViewModel(); 
     } 
//ViewModel 
    public class PersonViewModel : INotifyPropertyChanged 
     { 
      private Person _selectedPerson; 

      public List<Person> Persons { get; set; } 

      public Person SelectedPerson 
      { 
       get { return _selectedPerson; } 
       set { _selectedPerson = value; OnPropertyChanged("SelectedPerson"); } 
      } 

      public PersonViewModel() 
      { 
       Persons = new List<Person> 
       { 
        new Person(){Id = 1,BirthDate = DateTime.Now.AddYears(-30),Name = "Mark"}, 
        new Person(){Id = 2,BirthDate = DateTime.Now.AddYears(-40), Name = "Sophy"}, 
        new Person(){Id = 3,BirthDate = DateTime.Now.AddYears(-50), Name = "Bryan"}, 
       }; 
      } 




      public event PropertyChangedEventHandler PropertyChanged; 

      [NotifyPropertyChangedInvocator] 
      protected virtual void OnPropertyChanged(string propertyName) 
      { 
       PropertyChangedEventHandler handler = PropertyChanged; 
       if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

// Model  

    public class Person 
     { 
      public int Id { get; set; } 
      public string Name { get; set; } 
      public DateTime BirthDate { get; set; } 
     }