2016-05-12 36 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。 –

相關問題