WPF;将简单的字符串到我的TextBlock文本失败,因为无法找到资源命名为
所以我有这个TextBlock
:WPF;将简单的字符串到我的TextBlock文本失败,因为无法找到资源命名为
<TextBlock
Name="tbVersion"
Text="{Binding Converter={StaticResource TextConverter}}"/>
转换器:
public class TextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return "bla bla";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
,我得到这个错误时,尝试运行我的应用程序:
{“找不到名为'TextConverter'的资源。资源名称为 敏感。”}
虽然根本没有编译器错误,我声明这一点:
<Window.Resources>
<Convertors:TextConverter x:Key="TextConverter"/>
</Window.Resources>
行,所以我尝试采取另一种Converter
从另一个TextBlobk
工作,还是把这个错误是无法找到资源虽然这存在(和工程,我提到的...),所以也许是因为我的TextBlobk
里面DataTemplate
:
<Controls:MetroWindow.TitleTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
Name="tbVersion"
Text="{Binding Converter={StaticResource TextConverter}}"/>
</StackPanel>
</DataTemplate>
</Controls:MetroWindow.TitleTemplate>
正如你可以从图像中看到的,我使用了你的代码,并且工作得很好。
因此,您可能已将转换器添加到Window.Resources,但是您在不同的ResourceDictionary中使用它?
如果你想使他们在全球可用,你应该将它们添加到App.xaml中,或编程到Application.Current.ResourceDictionary
编辑:
尝试添加转换器到的DataTemplate .Resources
我在我的主XAML中使用此TextBlock和转换器声明在同一个XAML里面indow.Resources – user979033
这个TextBlock是在mahapps TitleTemplate里面的同一个XAML中声明的,请看我的更新 – user979033
好吧,现在可以开始工作了,谢谢! – user979033
你可以发布声明转换器的xaml吗?它是否在'ResourceDictionary'和/或'Resource'标签中? –
请看我的更新,这个在Window.Resources里面声明 – user979033
它对我有用!什么是你的转换器定义的名称空间 – tabby