2015-01-15 3 views
4

단추 클릭 후 "firstBox"및 "secondBox"에서 텍스트를 가져 오는 방법을 알지 못합니다.ListViewItem의 DataTemplate에서 TextBox로 텍스트를 가져 오는 방법

<ListView.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
     <!-- some code --> 
     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Data}" VerticalAlignment="Top" Height="18" Width="100" FontSize="13.333" Margin="162,9,0,0"/> 
     <TextBlock HorizontalAlignment="Left" Margin="0,35,0,0" TextWrapping="Wrap" Text="{Binding D_gospodarzy}" FontSize="14.667" VerticalAlignment="Top" Height="59" Width="100"/> 
     <TextBlock HorizontalAlignment="Center" Margin="268,35,7,0" TextWrapping="Wrap" Text="{Binding D_gosci}" FontSize="14.667" VerticalAlignment="Top" Width="100" Height="59"/> 
     <TextBox x:Name="firstBox" ... /> 
     <Button Content="Click" " Click="Button_Click_1"/> 
     <TextBox x:Name="secondBox" ... /> 
    </Grid> 
    </DataTemplate> 
</ListView.ItemTemplate> 

나는 단지 객체를 얻을

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    var myobject = (sender as Button).DataContext;    
} 
+0

이 질문과 제리의 HTTP에서 답변을 참조하십시오 :

private void Button_Click_1(object sender, RoutedEventArgs e) { var parent = (sender as Button).Parent; TextBox firstOne = parent.GetChildrenOfType<TextBox>().First(x => x.Name == "firstBox"); Debug.WriteLine(firstOne.Text); } 

것은 정적 클래스 어딘가에서 확장 메서드를 넣어 기억 : //stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate – Barnstokkr

답변

1

는 예를 들어, 당신은 클릭 버튼의 부모의 VisualTree을 통과하고 원하는 이름으로 텍스트 상자를 가져 오지 수, 그것을 할 수있는 방법의 cuple 있습니다. 이 경우 yasen in this answer으로 작성된 확장 메소드를 활용할 것입니다.

그 다음이 같은 예를 찾아보실 수 있습니다 :

public static class Extensions 
{ 
    public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class 
    { 
      // rest of the code 
0

.. 여기 텍스트를 얻을하는 방법

String text1 = firstBox.Text; 
String text2 = secondBox.Text; 

참고 :firstBoxsecondBox 반원들이 다른 그들을 사용해야합니다 클래스 메서드.

관련 문제