简体   繁体   English

在XAML中使用绑定设置样式

[英]Setting a Style using Binding in XAML

I need to set a Style of a given control according to some configuration value in Silverlight. 我需要根据Silverlight中的一些配置值设置给定控件的Style I would like to have a possibility of choosing a Style for a control from two available styles (static resources). 我想有选择的可能性Style从两个可用的样式(静态资源)的控制。 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). 你需要有约束力Style属性类型的属性Style虽然(不是代表一个静态资源查找的字符串)。

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: 将样式放在页面资源或应用程序资源中,然后您的ABC属性将看起来像以下之一:

// 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. _myStyle是样式在资源字典中为其x:Key属性具有的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM