通过C#codebehind中XAML中定义的StaticResource样式

通过C#codebehind中XAML中定义的StaticResource样式,c#,xaml,C#,Xaml,我正在构建一个WPF应用程序。XAML用于前端,C#用于代码隐藏 下面的代码部分动态地为我生成XAML if (station_item.Checker_Setup.First().Checker_Log.OrderByDescending(log => log.date).First().Status.status_key == 2) { Path path = new Path();

我正在构建一个WPF应用程序。XAML用于前端,C#用于代码隐藏

下面的代码部分动态地为我生成XAML

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);
                    } 
但是我在哪里呢

path.Style = "{StaticResource statusIndicatorRed}";
我得到以下错误

无法将类型字符串隐式转换为System.Windows.Style

样式在my MainWindow.xaml中定义如下

<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>
<Application.Resources>
    <ResourceDictionary Source="Styles.xaml" />
</Application.Resources>
path.Style = (Style)App.Current.Resources["statusIndicatorRed"];


如何在我的代码隐藏中传递这种风格?这是做事情的好方法吗

我就是这样解决这个问题的:

我创建了一个名为Styles.xaml的新资源字典

在我的App.xaml中,我引用了以下资源

<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>
<Application.Resources>
    <ResourceDictionary Source="Styles.xaml" />
</Application.Resources>
path.Style = (Style)App.Current.Resources["statusIndicatorRed"];

这就是我为解决问题所做的:

我创建了一个名为Styles.xaml的新资源字典

在我的App.xaml中,我引用了以下资源

<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>
<Application.Resources>
    <ResourceDictionary Source="Styles.xaml" />
</Application.Resources>
path.Style = (Style)App.Current.Resources["statusIndicatorRed"];

您需要将资源放置在代码隐藏设置可以访问的位置。例如
path.Style=(Style)App.Current.Resources[“statusIndicatorRed”]如果资源是在App.xaml或App.xaml.Aha引用的ResourceDictionary中定义的!我认为问题可能是代码背后没有办法看到这种风格的定义。感谢您的帮助,我将进行必要的更改。@Silvermind的最佳做法是在App.Xaml引用的单独文件中定义我的样式吗?是的,使用单独的资源字典对样式进行分组是一个好主意。您需要将资源放在您的代码隐藏设置可以访问的位置。例如
path.Style=(Style)App.Current.Resources[“statusIndicatorRed”]如果资源是在App.xaml或App.xaml.Aha引用的ResourceDictionary中定义的!我认为问题可能是代码背后没有办法看到这种风格的定义。感谢您的帮助,我将进行必要的更改。@Silvermind的最佳实践是在App.Xaml引用的单独文件中定义我的样式吗?是,使用单独的资源库来组合样式是一个好主意。+ 1考虑添加“代码>样式的相关部分。XAML< /代码>。+ 1考虑添加<代码>样式的相关部分。XAML < /代码>。