Selector Style and XAML Return Style

I created a style in XAML, how can I return this style to a style selector (code)?

I created a style in XAML and I only want to return the style declared in XAML.

+4
source share
3 answers

You need to access the XAML resource where you saved the style. This is usually a way to save this in a separate resource file. Then you need to access the URI of this XAML file as a ResourceDictionary object. Here is an example when I use a converter to decide which style the item will receive.

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(); } } } 
+7
source

You can add a property to your StyleSelector , and then use the property to pass a reference to Style in XAML.

 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> 
+8
source

If you're just looking for an example, here is relatively useful:

http://www.shujaat.net/2010/10/wpf-style-selector-for-items-in.html

If you have more specific questions, I would suggest posting some / XAML code to indicate what you tried and what problems you have.

+2
source

Source: https://habr.com/ru/post/1399215/


All Articles