How to set a StaticResource Binding in a window Title
Asked Answered
M

2

7

I want to put a IValueConverter on a binding to the title of a window, so that changes when the active project changes. The problem is that the value converter is a static resource, which is only loaded a few lines later:

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyProject"
        Height="600" Width="800" VerticalAlignment="Stretch"
        Title="{Binding ActiveProject, Converter={StaticResource windowTitleConverter}},  UpdateSourceTrigger=PropertyChanged">
     <Window.Resources>
         <local:MainWindowTitleConverter x:Key="windowTitleConverter"/>
     </Window.Resources>

     <!-- Rest of the design -->
</Window>

And then the definition of the converter:

public class MainWindowTitleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return "Programme"; else return "Programme: " + (value as string);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This crashes, presumably because the StaticResource hasn't been loaded yet (I can't think of any other reason), cause without the converter it works fine. However, I can't change the order. I tried to put it in a <Window.Title> tag, but anything that I put within that tag gives a compilation error. What is the proper way of doing this?

Meaningful answered 8/11, 2013 at 10:52 Comment(5)
What error do you get if you use <Window.Title>?Waggoner
Either Property Title does not have a value or Type <some class> was not found. But it doesn't help that I don't quite know how to use this tag, I admit.Meaningful
I vaguely recall running into issues with this particular property in the past, but I don't remember the details. I'll experiment when I get into the office.Waggoner
Sheridan saves the day yet again, but if you want another option you could do it in code, like in this question .Kaz
Turns out that indeed, I just didn't know how to use the <Window.Title> correctly.Meaningful
M
18

Just use the more verbose definitions

xmlns:System="clr-namespace:System;assembly=mscorlib"

...

<Window.Resources>
    <local:MainWindowTitleConverter x:Key="windowTitleConverter"/>
    ...
</Window.Resources>
<Window.Title>
    <Binding Path="ActiveProject">
        <Binding.Converter>
            <StaticResource ResourceKey="windowTitleConverter" />
        </Binding.Converter>
    </Binding>
</Window.Title>

I can't test this at the moment, but it should work.

Mortify answered 8/11, 2013 at 11:5 Comment(1)
Just a note: be sure to keep the definitions' order provided by the example above. (Add the converter(s) to the Resources before you use them).Buttocks
L
-2

The proper way would be to put the converter in your app.xaml.

Litharge answered 8/11, 2013 at 11:4 Comment(2)
I disagree - that puts MainWindow logic up at the application level and as demonstrated by @Sheridan, there is no need.Arrest
Well depends on the use case, in this specific case, propably ok. But now every window will have an instance of the Converter. Placing it in the app.xaml will create it once and everyone can use that one. Of course in this case this shouldn't be to much of a problem. But the implications should be clear. But to be clear, the app.xaml is there for widely reused resources. So these are usually best placed there, irregarding application level.Litharge

© 2022 - 2024 — McMap. All rights reserved.