Jiř*_*ála 11
您可以向您的属性添加属性StyleSelector,然后使用该属性将引用传递给StyleXAML.
public class MyStyleSelector : StyleSelector
{
private Style styleToUse;
public Style StyleToUse
{
get { return styleToUse; }
set { styleToUse = value; }
}
public override Style SelectStyle(object item, DependencyObject container)
{
return styleToUse;
}
}
<Control StyleSelector="{DynamicResource myStyleSelector}">
<Control.Resources>
<Style x:Key="myStyle">
...
</Style>
<local:MyStyleSelector x:Key="myStyleSelector" StyleToUse="{StaticResource myStyle}"/>
</Control.Resources>
</Control>
Run Code Online (Sandbox Code Playgroud)
您需要访问存储样式的XAML资源.通常,他们这样做的方法是将其存储在单独的资源文件中.然后,您需要访问该XAML文件的URI作为ResourceDictionary对象.这是一个示例,我使用转换器来决定元素将获得哪种样式.
namespace Shared.Converters
{
public class SaveStatusConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? saveState = (bool?)value;
Uri resourceLocater = new Uri("/Shared;component/Styles.xaml", System.UriKind.Relative);
ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
if (saveState == true)
return resourceDictionary["GreenDot"] as Style;
if (saveState == false)
return resourceDictionary["RedDot"] as Style;
return resourceDictionary["GrayDot"] as Style;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10345 次 |
| 最近记录: |