4
A
回答
7
您需要訪問存儲樣式的XAML資源。通常他們的方式是將其存儲在單獨的資源文件中。然後,您需要以該ResourceDictionary對象的身份訪問該XAML文件的URI。這裏是一個例子,我使用轉換器來決定一個元素的樣式。
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();
}
}
}
2
如果你只是尋找一個例子,這裏有一個相對可用的一個:
http://www.shujaat.net/2010/10/wpf-style-selector-for-items-in.html
如果你有更具體的問題,我會建議張貼一些代碼/ XAML表示你已經什麼嘗試和你有什麼問題。
8
您可以將屬性添加到您的StyleSelector
,然後使用屬性傳遞到在XAML中Style
的參考。
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>
相關問題
- 1. 使用StyleSelector和ItemContainerStyle
- 2. XAML組合樣式
- 3. XAML邊框樣式
- 4. 複製XAML樣式
- 5. 如何從另一個xaml樣式表中引用xaml樣式表
- 6. DataGrid行和單元格樣式在XAML
- 7. Silverlight XAML用戶控件和樣式
- 8. XAML和Silverlight:應用樣式子失敗
- 9. 回形針返回非現有樣式
- 10. Xaml繼承的樣式
- 11. WPF4 DataGridHeaderBorder在XAML樣式
- 12. C#樣式不使用xaml
- 13. Datagrid樣式的幫助XAML
- 14. WPF XAML樣式標記快捷方式?
- 15. 明確樣式覆蓋的隱式xaml樣式
- 16. js樣式屬性返回空白
- 17. wp_title()返回值無法樣式化
- 18. XLS樣式表返回空Excel表格
- 19. Rails無法返回腳本/樣式表
- 20. 樣式屬性:文本返回錯誤
- 21. XSLT樣式表不返回子元素
- 22. 將XAML樣式轉換爲樣式背後的代碼?
- 23. WPF的DataGrid StyleSelector的行
- 24. 如何測試WPF StyleSelector?
- 25. 在XAML中創建陰影樣式
- 26. XAML - 綁定組頭在組頭樣式
- 27. Silverlight中的XAML樣式不被識別
- 28. 確定XAML樣式的來源
- 29. 使用WPF中的XAML動態樣式
- 30. 雙值的XAML樣式觸發
我在XAML中創建了樣式,我只想返回在XAML中聲明的樣式。 – Never 2012-03-01 16:07:27