2011-06-24 63 views
2

需要一個建議(更好的從你的真實項目) - 在WPF中執行視覺不連貫的最佳方式是什麼?WPF視覺繼承

更具體的:如何使用狀態欄插入窗口視圖?

沒有辦法將另一個xaml文件插入其中。那麼,您是否創建了用戶控制MyStatusbar並將其粘貼到每個頁面上?

可以爲基本窗口創建樣式模板並使用樣式不一致,但這僅適用於簡單的可視屬性(顏色,大小)。 第二個想法是創建基礎DataTemplate,但沒有繼承。

P.S.在WinForms中,有一個帶有狀態欄和邏輯的基本表單。添加屬性後

public string StatusbarText {set{baseStatusbar.Text = value;}} 

在子窗體中使用屬性非常簡單。另外,我們還有查看狀態欄的繼承。

我知道如何在WPF中嵌入邏輯,但如何處理可視化。

回答

1

您當然可以創建自定義窗口控制,增加了一個StatusbarText屬性。或者,您可以使用自定義樣式窗口,唯一的問題是如何將狀態欄項傳遞到您的樣式。爲此,您可以使用attached properties

如果你走這條路線,你不能從默認的風格繼承你的風格,因爲你需要完全重新定義ControlTemplate。一種窗口樣式看起來像:

<ControlTemplate x:Key="WindowTemplateKey" 
       TargetType="{x:Type Window}"> 
    <Border Background="{TemplateBinding Background}" 
      BorderBrush="{TemplateBinding BorderBrush}" 
      BorderThickness="{TemplateBinding BorderThickness}"> 
     <Grid> 
      <AdornerDecorator> 
       <DockPanel> 
        <StatusBar DockPanel.Dock="Bottom" ItemsSource="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
        <ContentPresenter/> 
       </DockPanel> 
      </AdornerDecorator> 

      <ResizeGrip x:Name="WindowResizeGrip" 
         HorizontalAlignment="Right" 
         VerticalAlignment="Bottom" 
         Visibility="Collapsed" 
         IsTabStop="false"/> 
     </Grid> 
    </Border> 
    <ControlTemplate.Triggers> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="Window.ResizeMode" 
          Value="CanResizeWithGrip"/> 
       <Condition Property="Window.WindowState" 
          Value="Normal"/> 
      </MultiTrigger.Conditions> 
      <Setter TargetName="WindowResizeGrip" 
        Property="Visibility" 
        Value="Visible"/> 
     </MultiTrigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 


<Style x:Key="{x:Type Window}" 
     TargetType="{x:Type Window}"> 
    <Setter Property="Foreground" 
      Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
    <Setter Property="Background" 
      Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Window}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <AdornerDecorator> 
         <DockPanel> 
          <StatusBar DockPanel.Dock="Bottom" ItemsSource="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
          <ContentPresenter/> 
         </DockPanel> 
        </AdornerDecorator> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Window.ResizeMode" 
       Value="CanResizeWithGrip"> 
      <Setter Property="Template" 
        Value="{StaticResource WindowTemplateKey}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

如果你使用上面的樣式,你可以設置Window.Tag屬性是要顯示在狀態欄項目的列表。這種方法最大的問題是你需要爲StatusBar.ItemContainerStyle之類的東西添加附加屬性,這樣你可以自定義狀態欄的外觀。

如果您使用DataTemplate,同樣適用。所以我知道你只想在你的StatusBar中使用單個文本,你可以在上面的ControlTemplates中使用下面的代碼,並將Window.Tag設置爲字符串(或者使用附加屬性)。

<StatusBar DockPanel.Dock="Bottom"> 
    <StatusBarItem Content="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
</StatusBar> 
+0

有想法。謝謝。 有一些: 另外要mension約ContentPresenter是很重要使用Catel的例子http://www.codeproject.com/KB/WPF/Catel_Part5.aspx – Artru