Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:
C#
var window = new WindowButtonClick("Graphic") {DataContext = new GraphicViewModel()};
window.ShowDialog();

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:
C#
<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>

And here is a constructor of my WindowButtonClick:
C#
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?
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 11:58am    
Use the debugger and see what is the value of title and Application.Current.Resources[title] before assignment.
—SA
Alexander Dymshyts 10-Apr-14 3:20am    
Before assigment value of Title is empty.
Sergey Alexandrovich Kryukov 10-Apr-14 8:45am    
I asked about title, not Title; and you keep silence about Application.Current.Resources[title]. Can you debug comprehensively?
—SA
Alexander Dymshyts 10-Apr-14 10:54am    
Sorry for misunderstanding. title is "Graphic" and Application.Current.Resources[title] is value from resources with key - "Graphic". In debug it is "Graphic (us)".
After assigning value from resources Title is "Graphic (us)". Before assignment it was empty.
Sergey Alexandrovich Kryukov 10-Apr-14 11:22am    
Whatever it is — did you get right string from the resource? Did you get "Graphics (us)" from resource? Is it a correct value? If it is, what's the problem?
—SA

1 solution

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
 
Share this answer
 
Comments
Maciej Los 11-Apr-14 10:16am    
Quite long discussion (comments) to find quite "obvious" solution ;)
+5!
Sergey Alexandrovich Kryukov 11-Apr-14 10:35am    
Thank you, Maciej.
The technique was obvious enough to me, but the problem itself wasn't clear. It requires some research.
—SA
Maciej Los 11-Apr-14 11:16am    
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900