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}}" />
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步