开发者

WPF How to get the control in a nested template

开发者 https://www.devze.com 2023-01-25 22:21 出处:网络
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.

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 开发者_Python百科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;
0

精彩评论

暂无评论...
验证码 换一张
取 消