How do I access to a control element that is defined in a style
I've a CustomControl which use a ScrollViewer. The following code is an example which shows my use case in an easy way:
<Style TargetType="controls:MyCustomControl">
<Setter Property="Template"开发者_如何学C>
<Setter.Value>
<ControlTemplate TargetType="controls:MyCustomControl">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<ScrollViewer x:Name="scrollViewer" Grid.Row="0"
Height="300" Width="400"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
Style="{StaticResource ScrollViewerStyle}">
<Canvas x:Name="outer" Width="400" Height="400"
Background="Red" Margin="50">
<Canvas x:Name="inner" Background="Green"
Width="400" Height="400">
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding Value, ElementName=slider}"
ScaleY="{Binding Value, ElementName=slider}"/>
</Canvas.RenderTransform>
</Canvas>
</Canvas>
</ScrollViewer>
<Slider x:Name="slider" Minimum="0.1" Maximum ="10"
Value="1" Grid.Row="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
I left out the nonrelevant CodeBehind belonging to Slider.ValueChanged.
In a further question I ask How do I stick Controls inside of ScrollViewer. In this answer I got the idea to template the ScrollViewer. I added some elements to the ScrollViewer as shown in the answer. For example I've a new Canvas inside of my ScrollViewer:
<Canvas x:Name="canvas"/>
I need access to this Canvas at runtime, so in WPF I created:
public class MyScrollViewer : ScrollViewer
I override OnApplyTemplate
and get access to it via GetTemplateChild
. So all done.
In SL this way is not possible since ScrollViewer is sealed.
Now I am looking for an other way to get access. I hoped VisualTreeHelper would help me, but I do not get the Canvas. Neither in CodeBehind of my custom control nor in the codebehind of my MainPage (where I use my control).
private void GetCanvas(DependencyObject dependencyObject)
{
int count = VisualTreeHelper.GetChildrenCount(dependencyObject);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(dependencyObject);
Canvas canvas = child as Canvas;
if (canvas == null)
{
GetCanvas(child);
}
else
{
// Code here if found
}
}
}
Then a colleague remember me, that there exists the directive x:FieldModifier. I never used it before, just heard about it. I give it a try.
x:FieldModifier="public"
But no idea if it really give me the possibility to access from codebehind, even less how.
So, is there any way how I could access at my Canvas at runtime in Silverlight? Maybe there is maybe another solution in WPF itself, which also works on SL?
In Silverlight, this is possible using the extension method GetVisualDescendants(DependencyObject) in the Silverlight toolkit. The following should work...
- Add a reference to System.Windows.Controls.Primitives
- Add using System.Windows.Controls.Primitives
- Call scrollViewer.GetVisualDescendants().OfType().FirstOrDefault(canvas => canvas.Name.Equals("yourCanvasName")
精彩评论