2010-10-06 70 views
0

我有一个WPF窗口样式定义正是如此:WPF - 如何在每个屏幕上显示的文字

 <Style 
      x:Key="Applet" 
      TargetType="{x:Type Window}"> 
      <Setter 
       Property="WindowStyle" 
       Value="None" /> 
      <Setter 
       Property="WindowState" 
       Value="Maximized" /> 
      <Setter 
       Property="Title" 
       Value="Hindenburg" /> 
      <Setter 
       Property="FontFamily" 
       Value="Arial" /> 
      <Setter 
       Property="Height" 
       Value="650" /> 
      <Setter 
       Property="Width" 
       Value="850" /> 
     </Style> 

我的应用程序,然后定义使用这种风格(FlowWindow是刚刚从窗口衍生了一些额外的比特)的几个屏幕:

<uControl:FlowWindow 
x:Class="KaleidoscopeApplication.DisposablesScan" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:u="clr-namespace:KaleidoscopeApplication" 
xmlns:uControl="clr-namespace:KaleidoscopeApplication.Controls" 
Style="{StaticResource Applet}" 
Loaded="disposablesScanWindow_Loaded" 
Unloaded="disposablesScanWindow_Unloaded">  

<Canvas> 
    <!-- Top Bar Background --> 
    <Image 
     Source="Resources/Elements/Backgrounds/16.png" /> 

    text etc etc... 
</Canvas> 

我的问题 - 如何定义,将在使用这种风格的每个窗口显示的文本块?例如,如果我想要在每个屏幕的右上角显示徽标...

由于样式定义了大小和字体等内容,而不是画布的内容,所以我不确定如何去这个。

在此先感谢!

编辑:FlowWindow不是一个UserControl。它只是我的KaleidoscopeApplication.Controls命名空间的一部分。它被定义为:

public class FlowWindow : Window 
{  
    public FlowWindow() 
     : base() 
    { } 

    /// <summary> 
    /// Transition smoothly to another FlowWindow. 
    /// </summary> 
    /// <param name="toWindow">The window to transtion to.</param> 
    public override void Transition(FlowWindow toWindow) 
    { 
     ... 
    } 
} 
+0

什么是构成FlowWindow的“额外位”?这是否真的是一个用户控件(如前缀所暗示的),还是实际上是一个自定义控件,以及关联的generic.xaml文件? – 2010-10-06 18:59:28

+0

添加了一些关于FlowWindow的信息... – BabaBooey 2010-10-06 19:10:51

回答

2

您可以在FlowWindow类,它可以在设置中定义自定义依赖属性样式安装程序。例如,如果你创建了一个名为“LogoImage” LogoImageProperty,你可以从XAML绑定到它是这样的:

<Canvas> 
    <!-- Top Bar Background --> 
    <Image 
     Source="{Binding LogoImage, RelativeSource={RelativeSource Mode=Self}}" /> 

    text etc etc... 
</Canvas> 

这告诉FlowWindow使用本身作为结合语境,而不是DataContext的,但仅限于特定的约束力。


UPDATE:

由于您的FlowWindow只是一个逻辑包装(并且没有任何可视内容),你可能会考虑一些其他的可能性:

  1. 重新使用使用标准布局/样式为所有窗口单独定制Window类,并将当前窗口内容放入UserControls中。现在,您可以通过标准窗口上的ContentPresenter和DataTemplate托管特定的UserControl。如果您遵循MVVM模式,并且可以简单地传递视图模型以便通过Window对其进行可视化,则此功能尤其适用。

  2. 您可以使用您之后的布局为窗口创建一个新的ControlTemplate。有关更多详细信息,请参阅this answer

+0

事情是,没有XAML文件与FlowWindow类一起使用。我的示例中显示的XAML文件对应于称为DisposablesScan的单个屏幕。 – BabaBooey 2010-10-06 21:44:30

+0

啊,我明白了。我会提出一个替代解决方案,这是一个设计转变,但可能是有用的。 – 2010-10-06 23:29:04

2

如何制作窗口的基类,您可以在其中定义显示徽标和文本框的样式,使用数据绑定标题。然后在基本窗口中扩展应用程序中的其他窗口。

1

一种可能性是在FlowWindow的构造函数中添加一些东西。这是很难在这里给出一个完整的例子,因为我不知道您的具体设计,但这里是一些伪代码:

public FlowWindow() 
    : base() 
{ 
    Image logo = new Image(); 
    ImageSourceConverter converter = new ImageSourceConverter(); 
    string path = "pack://application:,,,/Resources/logo.png"; 
    ImageSource source = (ImageSource)converter.ConvertFromString(path); 
    logo.Source = source; 
    logo.Width = 50d; 

    // Add properties and attached properties like Canvas.LeftProperty, 
    // Canvas.TopProperty, Canvas.ZIndexProperty, etc., and then 
    // find the first child of the FlowWindow, and add the image 
    // to the Children collection of that first child 

}