2014-11-15 137 views
0

我实现了以下控制模板共享用户控件库WPF和控制模板

<ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}"> 
    <Border Name="Frame" Style="{StaticResource WindowBorder}" MouseLeftButtonDown="titleBar_MouseLeftButtonDown"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="50" /> 
       <RowDefinition Height="*" /> 
       <RowDefinition Height="40"/> 
      </Grid.RowDefinitions> 
      <controls:AppHeader Grid.Row="0" x:Name="Appheader" HeaderTitle="{Binding Path=AppTitle}" HeaderImageSource="{Binding Path=ImageSource}" ></controls:AppHeader> 
      <Border Grid.Row="1" BorderThickness="0,0,0,2" BorderBrush="#FFC1C1C1"> 
       <ContentPresenter /> 
      </Border> 
      <controls:AppNavigator Grid.Row="2" x:Name="AppNavigator"></controls:AppNavigator> 
     </Grid> 
    </Border> 
</ControlTemplate> 

<Style x:Key="WindowToolStyle" TargetType="Window"> 
    <Setter Property="Height" Value="400"/> 
    <Setter Property="Width" Value="600"/> 
    <Setter Property="WindowStyle" Value="None" /> 
    <Setter Property="ResizeMode" Value="NoResize"/> 
    <Setter Property="Topmost" Value="False"/> 
    <Setter Property="Template" Value="{StaticResource WindowTemplate}"/> 
</Style> 

我也有隐藏文件代码这个资源文件,来实现常用功能(如关闭,拖动窗口等)

public partial class WindowToolStyle : ResourceDictionary 
{ 
    public WindowToolStyle() 
    { 
     InitializeComponent(); 


    } 

    private void titleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     var window = (Window)((FrameworkElement)sender).TemplatedParent; 
     window.DragMove(); 
    } 

} 

然后我风格我的窗户全部用风格=“{StaticResource的WindowToolStyle}”

的问题是,我需要访问的声明对照的对象ol模板正在使用此模板进行样式化,我该怎么做?谢谢!

(我跟着这篇文章上面做http://www.codeproject.com/Articles/71485/Reusing-Control-Templates-in-Resource-Dictionaries,动机是创建可以使用我们的工具,让我们不要重复代码窗口模板)

+1

GetTemplateChild返回在你的样式中创建的任何命名对象(比如你的“AppHeader”)。 – gomi42

+0

这是行不通的,因为我的模板是在用户控制库中定义的,还有什么想法? – SFD

回答

-1

http://paulstovell.com/blog/wpf-part-names

使用teplatesPart属性在你的窗口中,然后在OnApplyTeplate()获取参考模板中的零件

+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 –