2010-08-10 8 views
9

정의 된 열과 행 세부 정보 템플릿이있는 DataGrid가 있습니다. 코드 내에서 행 세부 정보 템플릿의 컨트롤에 액세스하려면 어떻게합니까? 프로그래밍 방식으로 활성화/비활성화 할 버튼이 있지만 코드 뒤에 코드에 액세스하는 방법을 알 수 없습니다. 나는 MSDN에서 본 적이 :프로그래밍 방식으로 DataGrid 행 세부 정보 컨트롤에 액세스하는 방법

http://msdn.microsoft.com/en-us/library/bb613579.aspx

하지만 그냥 일반 데이터 템플릿을 설명하는, 그래서 내가 시도 할 때 작동하지 않았다. 내 경우는 행 세부 데이터 템플릿입니다. 분명히 누군가가이 코드에 대해 의견을 말할 수있는 DataGrid 행 세부 정보 템플릿 내의 컨트롤에 액세스하는 코드를 작성했습니다 (많이 감사하겠습니다).

답변

7

좋아, 나는 원래의 질문에 그 MSDN 문서에 게시 된 코드를 조정할했다이 작업 ....

DataGridRow row = (DataGridRow)(KeywordsGrid.ItemContainerGenerator.ContainerFromItem(KeywordsGrid.SelectedItem)); 

// Getting the ContentPresenter of the row details 
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row); 

// Finding Remove button from the DataTemplate that is set on that ContentPresenter 
DataTemplate template = presenter.ContentTemplate; 
Button button = (Button)template.FindName("RemoveItemButton", presenter); 

"KeywordsGrid"에 연결된 변수가 활용하는 방법을 알아 냈어 내 DataGrid. NoticeVisualChild에 대한 호출에서 "ContentPresenter"대신 "DataGridDetailsPresenter"클래스를 사용합니다 (키입니다 ... FindVisualChild 메서드가 모든 contentpresenters를 반복 할 때까지 강제로 처리해야만 세부 사항은 행 참조).

1

단추의 활성화 상태를 나타내는 표에 표시되는 개체 유형에 대한 속성을 정의 할 수 있습니까 (또는 이미 존재합니까?)? 그렇다면 버튼의 IsEnabled 속성을 해당 속성에 바인딩하기 위해 행 세부 템플릿을 수정하는 것이 훨씬 간단합니다.

+0

그래, 내보기 모델에서, 나는의 속성을 가질 수 DataGrid에 사용되는 클래스입니다. 그래서 이것은 이것을하는 한 가지 방법입니다. 클래스의 코드에서도이 작업을 수행하는 방법을 알았습니다. 나는 그것을 별도의 답변으로 게시 할 것이다. 귀하의 회신에 감사드립니다! – BrianP

1

DataGrid.LoadingRowDetails 이벤트를 사용하십시오! 그것은 훨씬 더 솔직합니다.

내가 여기를 발견 How to change Text of TextBlock which is in DataTemplate of Row Details for each DataGrid Row Details?

예 :

XAML

<DataGrid.RowDetailsTemplate> 
    <DataTemplate> 
     <TextBlock x:Name="Test">Test</TextBlock> 
     </DataTemplate> 
</DataGrid.RowDetailsTemplate> 

C#을

private void dgVehicles_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e) 
{ 
    TextBlock tbTest = e.DetailsElement.FindName("Test") as TextBlock; 
    if (tbTest != null) 
    { 
     tbTest.Text = "Juhuu"; 
    } 
} 
관련 문제