Search code examples
wpfmvvmsilverlight-5.0prism-4

Silverlight 5 ,Prism 5.1 StaticResource Use Not working of static class


I am unable to understand a thing which is as follows. My project is in Prism 4.1 Sliverlight 5. I'm Using MVVM pattern. I've a static class like this

{
    public static class RegionNames 
   {
     public static string AUTH_LOGIN_REGION = "AuthRegion";
     public static string TAB_TEST_REGION = "TabRegion";
     public static string USER_TAB_REGION="UserTabRegion";
    }
}

I tried to use this class in Shell.xmal like below.

    <Grid.Resources>            
        <inf:RegionNames x:Key="rName"></inf:RegionNames>
    </Grid.Resources>

Now this Resource I used in textblock
Result :No text appeared.

 <TextBlock Text="{Binding Source={StaticResource rNamee}, Path=USER_TAB_REGION}" Margin="20"></TextBlock>

Now I changed this class like below:

{
    public class RegionNames : INotifyPropertyChanged
   {
     public static string AUTH_LOGIN_REGION = "AuthRegion";
     public static string TAB_TEST_REGION = "TabRegion";
     public static string USER_TAB_REGION="UserTabRegion";

    public RegionNames() {
        AuthReginName = "HOLY COW POW POW !!";
    }

    private string _authReginName;
    public string AuthReginName {
        get {
            return _authReginName;
        }
        set {
            _authReginName = value;
            OnPropertyChanged("AuthReginName");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }
}

And used it like this

 <TextBlock Text="{Binding Source={StaticResource rNamee}, Path=AuthReginName}"></TextBlock>

Result : Text Appeared

this time it worked. Why? My static defied string values are not coming?. Is there any relation with Object creation of class & Setting Properties values?


Solution

  • First of all for instance objects you can bind only with properties, that's why second solution works for you. (You can't bind with fields)

    And for static properties you can bind with fields but you need to use x:Static markup extension. (same goes for properties as well)

    <TextBlock Text="{x:Static inf:RegionNames.USER_TAB_REGION}" Margin="20"/>