我有一個自定義控件,其中包含具有模板化工具提示的路徑。我希望能夠在運行時獲得對模板中網格的引用,以便根據使用情況修改它的子節點。如何在運行時修改Silverlight模板?
我想我可以使用GetTemplateChild從控件的OnApplyTemplate方法中獲取對模板中的網格的引用,但此方法未觸發。
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_tooltipDetails = (Grid)GetTemplateChild("TooltipDetailsGrid");
}
我還能怎麼做呢?
這是用戶控件的XAML。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="MiX.AssetManagement.UI.Timeline.Silverlight.TimelineBarRibbonItem"
FontSize="9.333" Foreground="White" mc:Ignorable="d">
<UserControl.Resources>
<ControlTemplate x:Key="ToolTipControlTemplateTimelineView" TargetType="ToolTip">
<StackPanel Orientation="Horizontal" Margin="16,0,0,0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="OpenStates">
<VisualState x:Name="Closed"/>
<VisualState x:Name="Open"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border CornerRadius="4">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#7F000000" Offset="0"/>
<GradientStop Color="#B2000000" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border x:Name="Info" Height="16" Width="16" BorderThickness="2" CornerRadius="8" HorizontalAlignment="Left" VerticalAlignment="Top">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#7FFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Path Fill="White" Stretch="Fill" Margin="5.229,2.089,5.035,2.82" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" Data="M0.77471197,5.0623446 L2.4198356,5.0623446 L2.4198356,10.18 L0.77471197,10.18 z M0.72914064,3.0891075 L2.4654069,3.0891075 L2.4654069,4.3332038 L0.72914064,4.3332038 z">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Border>
<ContentPresenter d:LayoutOverrides="Width, Height" Margin="20,0,0,0"/>
<Grid Grid.Column="1" x:Name="TooltipDetailsGrid">
<TextBlock Text="Tooltip in a Grid"/>
</Grid>
</Grid>
</Border>
</StackPanel>
</ControlTemplate>
</UserControl.Resources>
<Path x:Name="RibbonItem" Cursor="Hand">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF66CC33"/>
<GradientStop Color="#FF339900" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
<ToolTipService.ToolTip>
<ToolTip x:Name="RibbonItemTooltip" Content="" Foreground="#FFFFFFFF" FontSize="9.333" Placement="Mouse" Template="{StaticResource ToolTipControlTemplateTimelineView}"/>
</ToolTipService.ToolTip>
<Path.Data>
<GeometryGroup x:Name="RibbonItemGeometryGroup">
<RectangleGeometry x:Name="RibbonItemBackground" />
</GeometryGroup>
</Path.Data>
</Path>
</UserControl>
本文是添加源代碼的http://stackoverflow.com/questions/1497842/overriding-method-of-an-element-within-a-xaml-usercontrol的重新說明。基於OnApplyTemplate可能無法觸發的單一答案,因爲Path元素無法模板化。 – 2009-10-01 07:46:25
我沒有在這裏看到一個「自定義」控件,只是一個「用戶控件」? – AnthonyWJones 2009-10-01 09:14:46
謝謝你指出我濫用術語安東尼。雖然沒有幫助我解決我的問題。 – 2009-10-01 09:48:20