【问题标题】:WPF - How to display text on every screenWPF - 如何在每个屏幕上显示文本
【发布时间】:2010-10-06 17:48:42
【问题描述】:

我有这样定义的 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 只是从 Window 派生而来的,带有一些额外的位):

<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 不是用户控件。它只是我 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)
    {
        ...
    }
}

【问题讨论】:

  • 构成 FlowWindow 的“额外位”是什么?这真的是一个用户控件(正如您的前缀所暗示的那样),还是实际上是一个自定义控件,具有关联的 generic.xaml 文件?
  • 添加了一些关于 FlowWindow 的信息...

标签: c# wpf styles


【解决方案1】:

您可以在 FlowWindow 类上定义自定义依赖属性,这些属性可以在样式设置器中进行设置。例如,如果您创建了一个名为“LogoImage”的 LogoImageProperty,您可以像这样从 XAML 绑定到它:

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

    text etc etc...
</Canvas>

这告诉 FlowWindow 将自己用作绑定上下文而不是 DataContext,但仅用于该特定绑定。


更新:

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

  1. 为所有具有标准布局/样式的窗口重新使用单个自定义 Window 类,并将当前窗口内容放置在 UserControls 中。现在,您可以通过标准窗口上的 ContentPresenter 和 DataTemplate 托管特定的 UserControl。如果您遵循 MVVM 模式并且可以简单地传入视图模型以由您的 Window 可视化,这尤其适用。

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

【讨论】:

  • 问题是,FlowWindow 类没有 XAML 文件。我的示例中显示的 XAML 文件对应于名为 DisposablesScan 的单个屏幕。
  • 啊,我明白了。我将提出一个替代解决方案,它有点设计转变,但可能有用。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    一种可能是在 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
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      相关资源
      最近更新 更多