سؤال

So I've seen lots and lots of examples where someone has a nifty data template with a control in it and their content control is applying the template and their code behind needs to grab it.

But what I have is this:

<DataTemplate x:Key="frontTemplate" >
    <StackPanel x:Name="noWork">
        <Image Source="Images/1.png" Stretch="Fill" Width="72" Height="96" x:Name="FrontFace"   HorizontalAlignment="Left" VerticalAlignment="Top"></Image>
    </StackPanel>
</DataTemplate>

<DataTemplate x:Key="flipItemTemplate">
    <Grid Width="200" Height="200">
        <Border x:Name="frontHost" Background="Transparent">
            <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource frontTemplate}" />
        </Border>
    </Grid>
</DataTemplate>

You can see that I have a data Template (frontTemplate) nested inside of another data template (flipItemTemplate). What I need to do is get access to the Stackpanel in frontTemplate. All of my attempts to get to the content presenter for that datatemplate have failed. I am hoping that the wise sages of StackOverflow can help me. How in god's name would I get to that panel????

Thanks!!

هل كانت مفيدة؟

المحلول

If your XAML is defined like this:

<Grid Name="mainGrid">
    <Grid.Resources>
    <DataTemplate x:Key="frontTemplate" >
        <StackPanel x:Name="noWork">
            <Image Source="Images/1.png" Stretch="Fill" Width="72" Height="96" x:Name="FrontFace"   HorizontalAlignment="Left" VerticalAlignment="Top"></Image>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="flipItemTemplate">
        <Grid Width="200" Height="200">

            <Border x:Name="frontHost" Background="Transparent">
                <ContentPresenter Name="contentPresenter"  Content="{Binding}" ContentTemplate="{StaticResource frontTemplate}" />
            </Border>
        </Grid>
    </DataTemplate>
    </Grid.Resources>
</Grid>

You can use:

var template = mainGrid.FindResource("frontTemplate") as DataTemplate;
var stackPanel = template.LoadContent() as StackPanel;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top