WPF: How to access a control in style
Source:Open Clip Art Using Under Public Domain Attribution
I got a control in style template at one of my custom controls, Like this:
<Style TargetType="{x:Type advgenControls:CollapsibleControl}" x:Key="LeftPanel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type advgenControls:CollapsibleControl}">
<Grid>
<ContentPresenter x:Name="PART_ContentPresenter"/>
<ResizeGrip Grid.Row="1" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" HorizontalContentAlignment="Right" Margin="0,0,2,2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I just want to call that "PART_ContentPresenter". But that is xaml living in resources.
That is easy I can use GetTemplateChild("PART_ContentPresenter" ) and call at the OnApplyTemplate(). Please remember don't use it at OnInitialized(EventArgs e). At that time, styles has not loaded yet, you get a null object.:
Code
public override void OnApplyTemplate() | |
{ | |
base.OnApplyTemplate(); | |
if (this.IsInitialized) | |
{ | |
ContentPresenter cp = GetTemplateChild("PART_ContentPresenter" ) as ContentPresenter; | |
if(sections.Count >0){ | |
cp.Content = sections[0]; | |
} | |
} | |
} |
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 3877 feedbacks awaiting moderation...
Form is loading...