Binding to Resources in Silverlight/WPF

1. 对于静态资源,如下binding:


image

 

<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

aa4656eabf2851613210d9dab71e028e

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}}"  />

posted @ 2011-12-16 11:33  cubean  阅读(374)  评论(0编辑  收藏  举报