Hello,
I have an issue setting the labeltool foreground color to its default (or set theme) value. I have a WPF application using lots of Infragistics components, applying the default theme on all Infragistics parts.
My main issue is how to find the correct brush in code behind in a value converter.
public class PanelActiveConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return Binding.DoNothing; } if (value is PanelListItem) { var p = value as PanelListItem; if (p.Active == false) { return System.Windows.Media.Brushes.DarkRed; } else { return System.Windows.Media.Brushes.Black; } } if (value is PanelData) { var p = value as PanelData; if (p.Active == false) { return System.Windows.Media.Brushes.DarkRed; } else { return System.Windows.Media.Brushes.Black; } } return Binding.DoNothing; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } }
I want to set the foreground brush of the control using this converter depending on the Active value. If false, I want it to be dark red, if not I want to use the default color for that control, wether it is the default theme or any set theme in the future.
All I can see so far, is that Infragistics applies a default theme and uses this on its control. How can I access this in code behind?
LabelTool is the primary control this applies to, but such as grid cell editor will also need the same type of handling.
Br
Fredrik Walka
No there really is nothing that I'm aware of in the wpf framework that lets you get to what resource it might have used if one didn't have a property set that stopped the resolution. You could try to make assumptions like you are doing in one of the previous replies where you try to find the style and then enumerate the setters but that isn't foolproof as properties may have been set in triggers based on conditional state or in the case of inherited properties like Foreground may be coming from an ancestor element's property setting. So if using the wpf framework provided means of affecting the binding - i.e. returning Binding.DoNothing or DependencyProperty.UnsetValue - do not work for your scenario then you either would need to do something like you were trying to do (finding the style and enumerating setters, etc) or change your code so that you only use the converter when the object is in that state (e.g. use a trigger with a condition that only sets the property in question to the binding when the Active state is true).
Hi,
From my testing so far this does not seem to work. If foreground has already been changed, it will keep that value instead of changing back to the default.
Is there really no easier way to get the default value for foreground brush, based on the default or set theme used?
I was discussing the matter with the team and rather than trying to find out what the default color is you could just have the converter return Binding.DoNothing, just as you are doing for other scenarios in your converter.
Referring to this part:
Let me know if this help or need further assistance.
What I want is to be able to revert to the standard color/brush. I have tried some different things and I think I have a somewhat adaptable solution, but I find it less useful to use. Knowing the resource key(s) and how the get them easily would make it much more simple. The reason I ask about theme, is that the resource key may differ to the default key of the inherited control.
The code sample below works, but it is not pretty nor resource effective. Getting the resource key with a single line of code would be much better.
It should not be that hard to set foreground or any other smiliar property back to the default value when needed. That is basically all I want to do, restore the foreground to the default when a certain condition applies, otherwise set the foregorund to another value.
// Case LabelTool if (parameterType == typeof(LabelTool)) { var style = Application.Current.TryFindResource(typeof(LabelTool)) as Style; if (style != null) { var values = style.Setters; foreach (var item in values) { var i = item as Setter; if (i != null) { if (i.Property.Name == "Foreground") { var v = i.Value; if (v != null) { var h = ((DynamicResourceExtension)v).ResourceKey; var k = Application.Current.TryFindResource(h); if (k != null) { rKey = k; } } } } } }
Hello Fredrik,
Thank you for your post. It sounds like you are trying to get the default Foreground on the LabelTool? I'm not sure there's a way to do that ,once it's set, it will override whatever is in the theme.
Theme is just resource brushes and styles and stuff like that so once this gets set locally, there's nothing that holds on to it.
For example, so if you applies the MetroDark theme, the foreground color is like white or something. If you sets a Foreground of "Red," then there's nothing on that label that is going to return the color from the MetroDark theme because the Foreground is now set locally.