Reputation: 343
In my app resources i have:
<Application.Resources>
<System:Double x:Key="DefaultMargin">15.0</System:Double>
...
now in code behind i try to get the value
double? margin = Resources["DefaultMargin"] as double?;
but it's always null,
My question is: How can I set a const double in my app so that I can get it from xaml and C#
EDIT: tip: I'm working on WP7/8
Upvotes: 0
Views: 135
Reputation: 31626
Define the value in question on your ViewModel (or whatever your page's context is set to which adheres to INotifyPropertyChanged) and access it that way on both levels.
If you have to access it on the resource then set it like:
myWindow.Resources.Add("myResourceKey", myViewModel.myValue);
access it in xaml as via a StaticResource
"{StaticResource myResourceKey}"
or as mentioned even binding
"{Binding myValue}"
Hence there are three ways to get at the value.
Upvotes: 0
Reputation: 73472
Am not sure about silverlight but in wpf this works. You need to access App.Current.Resources
since you're storing it there.
double? margin = App.Current.Resources["DefaultMargin"] as double?;
Upvotes: 1