Setting a Style using Binding in XAML
I need to set a Style
of a given control according to some configuration value in Silverlight. I would like to have a possibility of choosing a Style
for a control from two available styles (static resources). I 开发者_如何转开发was trying to do something like:
<TextBox Style="{Binding ABC}"/>
where
public string ABC
{
get {return "{StaticResource MyStyle}";}
}
Unfortunately, that does not work.
Do you have any ideas?
Thanks in advance!
Cheers
You are close. You need to be binding the Style
property to a property of type Style
though (not a string representing a static resource lookup).
You have two options for storage of the style and this will determine what the property looks like. Either put the style in the pages resources or in the App resources, and then you ABC property will look like one of the following:
// using page resources
public Style ABC
{
get { return (Style) this.Resources["_myStyle"]; }
}
// using application resources
public Style ABC
{
get { return (Style) App.Current.Resources["_myStyle"]; }
}
Where _myStyle
is the value the style has for its x:Key
property in the resource dictionary.
精彩评论