Silverlight - Get the ItemsControl of a DataTemplate

Posted by user208662 on Stack Overflow See other posts from Stack Overflow or by user208662
Published on 2010-02-27T17:10:23Z Indexed on 2010/03/13 0:47 UTC
Read the original article Hit count: 338

Filed under:

Hello,

I have a Silverlight application that is using a DataGrid. Inside of that DataGrid I have a DataTemplate that is defined like the following:

<Grid x:Name="myGrid" Tag="{Binding}" Loaded="myGrid_Loaded">
  <ItemsControl ItemsSource="{Binding MyItems}" Tag="{Binding}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <StackPanel Orientation="Horizontal" Width="138">
            <TextBlock Text="{Binding Type}" />
            <TextBox x:Name="myTextBox" TextChanged="myTextBox_TextChanged" />
          </StackPanel>
        </StackPanel>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

When a user enters text into the TextBox, I have an event (myTextBox_TextChanged) that must be fired at this point. When that event gets fired, I would like to get the ItemsControl element that is the container for this TextBox. How do I get that ItemsControl from my event handler?

Please note: Because the ItemsControl is in the DataTemplate of DataGrid, I don't believe I can just add an x:Name and reference it from my code-behind. Or is there a way to do that?

Thank you!

© Stack Overflow or respective owner

Related posts about Silverlight