2016-05-12 38 views
1

我已经通过重新定义它并创建了一个自定义窗口模板,并从窗口继承。这个模板驻留在一个单独的类库中,我构建了一个dll并从我的主项目中引用它。
这是代码自定义窗口XAML的一部分:xaml(wpf)如何解决我的自定义窗口模板中的控件?

 <!-- Window style --> 
     <Style TargetType="{x:Type local:CustomWindow}"> 
      <Setter Property="WindowStyle" Value="None"/> 
      <Setter Property="ResizeMode" Value="NoResize"/> 
      <Setter Property="Background" Value="White"/> 
      <Setter Property="AllowsTransparency" Value="True"/> 
      <Setter Property="Opacity" Value="1" /> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="BorderBrush" Value="Silver"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type local:CustomWindow}"> 
         <Border BorderThickness="{TemplateBinding BorderThickness}" 
           BorderBrush="{TemplateBinding BorderBrush}"> 
          <Grid> 
           <Grid> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="auto"/> 
             <RowDefinition /> 
            </Grid.RowDefinitions> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="Auto" /> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition Width="Auto"/> 
            </Grid.ColumnDefinitions> 
            <Rectangle x:Name="moveRectangle" Fill="#24282A" 
              Grid.Row="0" Grid.Column="1"/> 
            <Label x:Name="WindowName" Background="#24282A" Foreground="White" Grid.Row="0" Grid.Column="0"/> 
            <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal" Background="#24282A"> 
             <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}" 
               Content="0" /> 
             <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}" 
               Content="1" /> 
             <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}" 
               Content="r" /> 
            </StackPanel> 
            <Grid Background="#24282A" Opacity="0.9" 
              Grid.Row="1" Grid.ColumnSpan="3" Margin="5,0,5,5"> 
             <AdornerDecorator> 
              <ContentPresenter/> 
             </AdornerDecorator> 
            </Grid> 
           </Grid> 
           <Grid x:Name="resizeGrid"> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            VerticalAlignment="Top" 
            Height="5" 
            x:Name="top" 
            Margin="5,0,5,0" /> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            x:Name="bottom" 
            Height="5" 
            VerticalAlignment="Bottom" 
            Margin="5,0,5,0" /> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            HorizontalAlignment="Left" 
            Margin="0,5,0,5" 
            Width="5" 
            x:Name="left"/> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            Margin="0,5,0,5" 
            Width="5" 
            HorizontalAlignment="Right" 
            x:Name="right" /> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            HorizontalAlignment="Left" 
            VerticalAlignment="Bottom" 
            Width="5" 
            Height="5" 
            x:Name="bottomLeft" /> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            VerticalAlignment="Bottom" 
            Height="5" 
            Width="5" 
            HorizontalAlignment="Right" 
            x:Name="bottomRight" /> 
                 <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            HorizontalAlignment="Right" 
            Width="5" 
            Height="5" 
            VerticalAlignment="Top" 
            x:Name="topRight" /> 
                <Rectangle 
            Stroke="{x:Null}" 
            Fill="#24282A" 
            HorizontalAlignment="Left" 
            Width="6" 
            VerticalAlignment="Top" 
            Height="5" 
            x:Name="topLeft" /> 
           </Grid> 
          </Grid> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

正如你可以看到,有一个叫“WindowName”的标签。我希望这个标签在我的自定义窗口中是一种标题栏,我想从我的主wpf应用程序中调用它的属性内容,该应用程序从此自定义窗口继承。一切工作正常,除了我不知道我应该如何调用这个标签来设置它的内容。任何帮助,将不胜感激

回答

1

你会想要将Label的内容绑定到Window类的Title属性,因为基类已经有一个可以重用的依赖属性。所有你需要做的是给你的标签组件,如下更新XAML:

<Label x:Name="WindowName" Content={TemplateBinding Title} Background="#24282A" Foreground="White" Grid.Row="0" Grid.Column="0"/> 

你也可以覆盖你的CustomWindowOnApplyTemplate并使用像FindName的方法使用其名称来获得Label,然后通过更新它一个直接的参考,但绑定方式更清洁,所以我不会考虑这条路线,尽管我想至少提到它。

+0

Thx,它帮助我:)这是我想要的。我可以问你一些警告, 'CustomWindow.Title'隐藏继承的成员'Window.Title'。如果需要隐藏,请使用新关键字。 添加新的更好吗? –

+0

好点。 Window已经有一个Title依赖项属性,所以你不需要去创建一个新的问题(假设你只想使用字符串作为标题)。我会更新我的答案,你需要做的就是更新xaml。 –

相关问题