I am building a WPF application. XAML used for front end and C# for code behind
I have the following section of code that generates my XAML for me dynamically.
if (station_item.Checker_Setup.First().Checker_Log.OrderByDescending(log => log.date).First().Status.status_key == 2) { Path path = new Path(); path.Data = new RectangleGeometry(new Rect(0, 0, 19, 21), 3, 3); path.Style = "{StaticResource statusIndicatorRed}"; TextBlock block = new TextBlock(); block.Text = station_item.station_name; WrapBox.Children.Add(path); WrapBox.Children.Add(block); } However where I have
path.Style = "{StaticResource statusIndicatorRed}"; I get the following error
Cannot implicitly convert type String to System.Windows.Style
The style is defined in my MainWindow.xaml as follows
<Style x:Key="statusIndicatorRed" TargetType="Path"> <Setter Property="Fill" Value="#B2203D" /> <Setter Property="Width" Value="19px" /> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="ToolTipService.ShowDuration" Value="30000" /> <Setter Property="Cursor" Value="Help" /> </Style> How would I pass this style through in my code behind? Is this even a good way to do things?
path.Style = (Style)App.Current.Resources["statusIndicatorRed"];if the resource is defined in App.xaml or in a ResourceDictionary referenced by App.Xaml.