WPF How to access control from DataTemplate
In above example, the answers are all related to .Net solution. I am looking for similar solution for silverlight, it does not have FindName on Framework Template. My DataTemplate has a grid which I need to use to open the selecteditem on the grid. I currently have 4 grids which are placed using contentpresenter and since Silverlight does not support TemplateSelector I have visibility set to hide and show correct content presenter. Is there a way I can access Grid within my data template?
<DataTemplate x:Key="DefaultTemplate">
<toolkit:DataGrid x:Name="dgGrid"
ItemsSource="{Binding List}"
HorizontalAlignment="Stretch"
HeadersVisibility="Column"
ColumnHeaderHeight="30"
VerticalGridLinesBrush="Transparent"
AutoGenerateColumns="False"
SelectionMode="Single"
RowStyle="{StaticResource rowStyle}"
FontSize="16"
IsReadOnly="True">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTemplateColumn Width="Auto" SortMemberPath="IsInvalid" >
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="!" Visibility="{Binding IsInvalid, Converter={StaticResource conv}}"/>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
<!--Open Button-->
<toolkit:DataGridTemplateColumn Width="70">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Open"
Width="60"
FontSize="14"
VerticalAlignment="Center" HorizontalAlignment="Center"
Margin="0"
Click="Open_Click"
/>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
<toolkit:DataGridTextColumn Header="Name" Width="150" FontSize="18" Binding="{Binding CustomerName}" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</DataTemplate>
I am trying to access dgGrid on above DataTemplate
Updated:
In my control I have following
<StackPanel>
<ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template1}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=5}"/>
<ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template2}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=7}"/>
<ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource Template3}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=13}"/>
<ContentPresenter Content="{Binding}" DataContext="{Binding}" ContentTemplate="{StaticResource DefaultTemplate}" Visibility="{Binding PanelID,Converter={StaticResource ParamVisibilityConv}, ConverterParameter=4}"/>
</StackPanel>
I am trying to access the grid in code behind
StackPanel sp = (StackPanel) this.Content;
foreach (var child in sp.Children)
{
if (child is ContentPresenter && ((ContentPresenter) child).Visibility == Visibility.Visible)
{
DataTemplate dt = ((ContentPresenter) child).ContentTemplate;
}
}
it returns a DataTemplate but FrameworkTemplates dont have FindName function to find elements within a DataTemplate.
Is there a workaround?