2011-06-24 64 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