我有一个WPF窗口与一些文本框和一个DataGrid。 DataGrid中充满了数据,但我需要它,当用户单击该数据网格中的单元格时,程序将检测该行并用该行中的数据重新填充文本框。 例如,有一个ID,Name和BirthDate文本框。当用户单击给定行中的任何单元格时,文本框ID,Name和BirthDate的值必须成为所选行中各列(ID,Name,BirthDate)的值。 我环顾四周寻找答案,但我似乎只能找到与WinForms相关的答案,并且WinForms和WPF中的DataGrid工作方式完全不同,代码明智。C# - 从WPF DataGrid填充文本框
1
A
回答
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; }
}
相关问题
- 1. C#WPF:填充DataGrid失败
- 2. 填充WPF的DataGrid从XML
- 3. wpf删除datagrid左填充
- 4. WPF DataGrid HeaderTemplate神秘填充
- 5. 在WPF中填充DataGrid
- 6. 动态填充WPF DataGrid
- 7. WPF UI自动填充文本框
- 8. WPF填充treeview选项到文本框
- 9. 使用C#2005中的文本框填充Datagrid
- 10. 从webservices填充datagrid
- 11. 文本框填充
- 12. 从Datagrid(WPF)绑定文本框
- 13. 从C#中的MySQL数据库填充dataGrid WPF
- 14. 从文本框填充列表框
- 15. 如何在使用List动态填充wpf datagrid列文本?
- 16. 从DropDownList填充文本框选择用CascadingDropDown填充本身
- 17. 从XML文件填充文本框
- 18. 从文件填充文本框
- 19. 从文本文件填充选择框
- 20. C#OleDb dataGridView填充文本框
- 21. 在datagrid行上填充文本框点击
- 22. WPF在DataGrid中填充整数值的组合框
- 23. C从文本文件填充数组
- 24. wpf datagrid文本框+组合框
- 25. 在WPF用户控件中填充Datagrid
- 26. 自动填充文本框
- 27. 自动填充文本框
- 28. 预填充dynaform文本框
- 29. 填充文本框与
- 30. 从XML文件填充datagrid或listview
[教程](http://www.codeproject.com/Articles/332615/WPF-Master-Details-MVVM-Application)。你的问题对于Stackoverflow来说太宽泛了。顺便说一句winforms糟透了。 –
我知道它很烂,这就是为什么我使用WPF。我会考虑你给我的那个链接。 – chaosflaw
需要了解有关MVVM模式。 MVVM将“经常”使WPF更轻松。 – HichemSeeSharp