将标题设置为窗口。 [英] Set title to the window.

查看:77
本文介绍了将标题设置为窗口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很有趣,但我找不到我错的地方......

所以我有2个窗户。从第一个窗口开始,我在按钮点击事件中调用第二个:

It's funny, but I cann't find where I am wrong...
So I have 2 windows. From the first window I'm calling second on button click event:

var window = new WindowButtonClick("Graphic") {DataContext = new GraphicViewModel()};
window.ShowDialog();



第二个窗口是一个窗口,我加载不同的视图(像userControls一样创建)。这是一个例子,如何在Xaml中调用其中一个:


Second window is a window, where I load different views( created like userControls). Here is example, how do I call one of them in Xaml:

<Window x:Class="WindowButtonClick"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graphic="clr-namespace:Windows.Graphic"
WindowStartupLocation="CenterScreen" >
<Window.Resources>
      <DataTemplate DataType="{x:Type graphic:GraphicViewModel}">
        <graphic:Graphic />
      </DataTemplate>
</Window.Resources>
<Grid>
    <ContentControl Content="{Binding}"/>
</Grid>



这里是我的WindowButtonClick的构造函数:


And here is a constructor of my WindowButtonClick:

InitializeComponent();
Title = Application.Current.Resources[title].ToString();



标题设置为资源的值,但我在创建的窗口上看不到标题..



任何想法有什么问题?


Title is set to a value from resource, but I cann't see Title on the created window..

Any ideas what's wrong?

推荐答案

感谢您按照我的评论中显示的方式测试您的代码。



因此,它确认了 Window.Title的值已正确计算,但稍后被覆盖。如果在窗口生命周期的后续步骤中执行此操作,则可以确保分配和呈现新值,在其上下文已经呈现时,就在首次显示之前。实现这一目标的方法之一是覆盖虚拟方法 System.Windows.Window.OnContentRendered 并在那里移动您的作业。请参阅:

http://msdn.microsoft .com / zh-cn / library / system.windows.window.oncontentrendered.aspx [ ^ ]。



有很多情况需要你做触摸到窗户;这种方式可能是最简单的。



-SA
Thank you for testing your code the way shown in my comment.

So, it confirmed that the value for the Window.Title was calculated correctly, but is later overwritten. You can make sure that the new value is assigned and rendered, if you do it on the later step of the window life cycle, when its context is already rendered, right before is is first shown. One of the ways to achieve that is to override the virtual method System.Windows.Window.OnContentRendered and move your assignment there. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.window.oncontentrendered.aspx[^].

There are many situations where you need to do "last touches" to a window; and this way is probably the easiest.

—SA


这篇关于将标题设置为窗口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆