6
A
답변
6
필립이 말한 바에 따르면 DataGrid
은 일반적으로 데이터 바인딩입니다. 다음은 내 WPF DataGrid
이 ObservableCollection<PersonName>
에 바인딩 된 예입니다. 여기서 PersonName
은 FirstName
및 LastName
(두 문자열)로 구성됩니다.
DataGrid
은 자동 열 작성을 지원하므로 예제가 매우 간단합니다. 인덱스를 기준으로 행에 액세스하고 열 이름에 해당하는 속성 이름을 사용하여 해당 행의 셀 값을 가져올 수 있습니다.
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
// Create a new collection of 4 names.
NameList n = new NameList();
// Bind the grid to the list of names.
dataGrid1.ItemsSource = n;
// Get the first person by its row index.
PersonName firstPerson = (PersonName) dataGrid1.Items.GetItemAt(0);
// Access the columns using property names.
Debug.WriteLine(firstPerson.FirstName);
}
}
public class NameList : ObservableCollection<PersonName>
{
public NameList() : base()
{
Add(new PersonName("Willa", "Cather"));
Add(new PersonName("Isak", "Dinesen"));
Add(new PersonName("Victor", "Hugo"));
Add(new PersonName("Jules", "Verne"));
}
}
public class PersonName
{
private string firstName;
private string lastName;
public PersonName(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
}
1
일반적으로 DataGrid 셀의 내용은 데이터 바인딩이므로 주어진 행에 표시되는 개체의 속성 (대부분의 경우)의 상태를 반영합니다. 따라서 보기보다는 모델에 액세스하는 것이 더 쉬울 수도 있습니다.
내 모델에 대한 질문은 다음과 같습니다. 당신은 무엇을하려고합니까? 화면에 렌더링되는 컨트롤 (또는 컨트롤)을 찾기 위해 시각적 트리를 가로 지르는 방법은 입니까? 행 및 열 인덱스를 사용하여 셀을 어떻게 참조 할 것으로 예상합니까?
1
DataTable을 사용하여 바인딩하는 경우 Row 's Item 속성에서 DataRowView를 가져올 수 있습니다.
DataRowView rowView = e.Row.Item as DataRowView;
나는 약간의 소리가 들리 겠지만 WPF를 처음 사용하고 DataGridView를 사용하여 Forms에서와 같은 작업을 시도하고 비슷한 결과를 얻지 못했습니다. 그래서 상세한 솔루션은 정말 감사하겠습니다! – Partial
어쩌면 단순하지 않을 수도 있습니다. – Partial
WindowsFormsIntegration을 사용하여 Forms의 DataGridView를 사용한다고 생각합니다. – Partial