将cs代码中的样式分配给wpf中的tabitem

时间:2016-08-02 10:23:00

标签: wpf xaml computer-science

嗨,我已经在xaml中创建了一个自定义样式:

<Application.Resources>
        <Style x:Key="TabEnabledEffect" TargetType="{x:Type TabItem}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="BitmapEffect">
                        <Setter.Value>
                            <DropShadowBitmapEffect ShadowDepth="10" Color="Orange" Direction="90" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>

在cs文件中 我有一个名为&#34; tab1&#34;并尝试使用以下代码在button_click事件上为其指定该样式:

tab1.Style = (Style)FindResource("TabEnabledEffect");

运行后即可获得异常:

的NullReferenceException {&#34;对象引用未设置为对象的实例。&#34;}

请帮忙。 提前致谢

2 个答案:

答案 0 :(得分:0)

这取决于您在代码中的位置。在您的代码隐藏文件中,您可以使用您拥有的代码行:

tab1.Style = this.FindeResource("TabEnabledEffect") as Style;

当你在另一个文件中时,你必须使用@bit:

提到的语句
tab1.Style = Application.Current.FindResource("TabEnabledEffect") as Style;

答案 1 :(得分:0)

您可能想要使用

Application.Current.FindResource("TabEnabledEffect") as Style;

因为您已在应用程序级别声明了资源。

相关问题