Binding to Resources in Silverlight/WPF
1. 对于静态资源,如下binding:
<UserControl.Resources> <l:i18n x:Key="i18n" /> </UserControl.Resources> <StackPanel x:Name="LayoutRoot" Orientation="Horizontal"> <TextBox /> <Button Content="{Binding Path=SearchButton, Source={StaticResource i18n}}" /> </StackPanel>
但对于自定义的资源,like
namespace ResourceProjectNamespace {
public class ApplicationStrings {
internal ApplicationStrings() {
}
public static string BtnShow {
get {
return ResourceManager.GetString("BtnShow", resourceCulture);
}
}
}
}
如果按以上写,程序运行时会报错:No matching constructor found on type …
所以对这类资源的binding需要用以下方式:
2. 自定义资源的binding
xmlns:resources="clr-namespace:ResourceProjectNamespace"
<Button Content="{Binding Source={x:Static resources:ApplicationStrings.BtnShow}}" />