我想在窗口标题的绑定上使用IValueConverter
,以便在活动项目更改时更新。但问题是值转换器是静态资源,只有在几行后才加载:
<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>
接下来是转换器的定义:
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();
}
}
这个程序崩溃了,可能是因为StaticResource
没有被加载(我想不到其他原因),因为没有转换器时它可以正常工作。然而,我不能改变顺序。我试图将它放在 <Window.Title>
标签中,但是无论我在标签中放什么都会出现编译错误。做这件事的正确方法是什么?
<Window.Title>
,会出现什么错误? - Mike Strobel<Window.Title>
。 - Yellow