I am trying to set the style that is defined in App.xaml dynamically when the user control is loaded, and for some reason it does not apply the style (i.e. there is no error, it just does not apply the style).
I am sure because I did not define the binding correctly, but I cannot understand what I need to do differently to make it work.
App.xaml style
The style that I am is RunningTitleBlock , and it consists of a couple of other styles that I included in the code example below.
<Style TargetType="Label"> <Setter Property="Margin" Value="4"/> </Style> <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}" x:Key="HeaderBlock"> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="White"/> </Style> <Style TargetType="Label" BasedOn="{StaticResource ResourceKey=HeaderBlock}" x:Key="TitleBlock"> <Setter Property="Foreground" Value="Black"/> </Style> <Style TargetType="Label" BasedOn="{StaticResource ResourceKey=TitleBlock}" x:Key="RunningTitleBlock"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0.0, 0.5" EndPoint="1.0, 0.5"> <GradientStop Color="White" Offset="0.0"/> <GradientStop Color="Green" Offset="1.0"/> </LinearGradientBrush> </Setter.Value> </Setter> </Style>
user management binding
I am trying to bind a Binding to a value returned from a value converter.
Style="{DynamicResource ResourceKey={Binding Path=MonitoringType, Converter={StaticResource TSConverter}}}"
Code
MonitoringTypes Enum
public enum MonitoringTypes { Running, Failed, Paused, Favorites, }
User control
Here, what I'm trying to do is concatenate the string value of the MonitoringTypes enum value, which is passed with some well-known text to create a style name that exists in App.xaml . The value converter is called and , returning the correct value, but for some reason the style is not applied.
/// <summary> /// Interaction logic for MonitorWorkflow.xaml /// </summary> public partial class MonitorWorkflow : UserControl { public MonitorWorkflow(MonitoringTypes monitoringType) { InitializeComponent(); this.DataContext = new MonitorWorkflowViewModel { MonitoringType = monitoringType }; } } public class MonitorWorkflowViewModel { public MonitoringTypes MonitoringType { get; set; } } public class TitleStyleValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var type = (MonitoringTypes)value; return string.Format("{0}TitleBlock", Enum.GetName(typeof(MonitoringTypes), type)); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Enum.Parse(typeof(MonitoringTypes), value.ToString().Substring(0, value.ToString().IndexOf("TitleBlock"))); } }