tags:

views:

49

answers:

3

If I have a UserControl hosted in a Window that looks like:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
    <Grid>
        <ContentPresenter Content="{Binding}" />
    </Grid>
</Window>

How do I set the Window.Title from the UserControl?

An additional requirement is that the UserControl can pull and set the window title from a static resx file such, i.e. {x:Static p:Resources.MyViewTitle}

Edit

After doing some more research, I think Attached Properties might be the answer. How might I go about implementing a Window.Title property that I can place on any child UserControl, allowing me to set the Window title?

A: 

You could create an event for the UserControl that the parent window subscribes to.

Mike Pateras
As the two are decoupled fairly well there is no place to hook up the events.
Daniel Skinner
+2  A: 

(Untested): Use VisualTreeHelper.GetParent to traverse the visual tree upwards until you reach an object of type Window. Then set the Title to Resources.ResxFileName.MyViewTitle (should be available in Code due to the auto-generated Resource classes).

Heinzi
Is there a XAML-only solution?
Daniel Skinner
Good question. I guess you could use `{Binding RelativeSource={RelativeSource Mode=FindAncestor, Type={x:Type Window}}, Path=Title` to bind to the Title, and use `{x:Static p:Resources.MyViewTitle}` to bind to the value, but I'm not sure how you can *connect* these two bindings...
Heinzi
Yeah, that's about as far as I got too
Daniel Skinner
Searching the parent tree is how I endedup doing it. I made it tidy by implementing it as an attached property.
Daniel Skinner
A: 

The answer is to create an attached property. See: http://stackoverflow.com/questions/2176655/creating-a-window-title-attached-property/

Daniel Skinner