WPF: InvalidCastException: Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.ResourceDictionary'

1

I try to re-style TextBoxes in my Control to have the same look for ReadOnly as if they were Disabled. Last week I could achieve this without any problem like this:

<UserControl.Resources>
    <Style TargetType="{x:Type Border}" x:Key="TextBoxBorderStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBoxBase}}" Value="True">
                <Setter Property="Opacity" Value="0.56"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

... and use it in TextBoxes like this:

<TextBox Text="{Binding MyText}"
         IsReadOnly="{Binding ReadOnly}"
         VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" >
    <TextBox.Resources>
        <StaticResource ResourceKey="TextBoxBorderStyle" />
    </TextBox.Resources>
</TextBox>

I don't remember changing anything in this control in the meantime. (Though I updated Visual Studio 2019 to 16.9.4 today). But when I run my application now, it gives a run time error:

System.Windows.Markup.XamlParseException HResult=0x80131501
Message='Set property 'System.Windows.FrameworkElement.Resources' threw an exception.' Line number '90' and line position '33'.
Source=PresentationFramework StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)

This exception was originally thrown at this call stack: System.Windows.Baml2006.WpfSharedBamlSchemaContext.Create_BamlProperty_FrameworkElement_Resources.AnonymousMethod__276_0(object, object) MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(object, System.Xaml.XamlMember, object)

Inner Exception 1: InvalidCastException: Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.ResourceDictionary'.

It works as it should when I copy-paste the style directly into all of the Textbox.Resources . Anyone knows why? Especially since sharing the StaticResource has worked without a problem before.

EDIT: It works as expected in the xaml editor/designer and properly adjusts the style to the view model data without any exception. Only at run time there is a problem.

c#
wpf
windows
xaml
exception
asked on Stack Overflow Apr 19, 2021 by Remco • edited Apr 19, 2021 by Remco

2 Answers

1

Replace

    <TextBox Text="{Binding MyText}"
             IsReadOnly="{Binding ReadOnly}"
             VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" >
        <TextBox.Resources>
            <StaticResource ResourceKey="TextBoxBorderStyle" />
        </TextBox.Resources>
    </TextBox>

With

    <TextBox Text="{Binding MyText}"
             IsReadOnly="{Binding ReadOnly}"
             VerticalScrollBarVisibility="Disabled" 
             HorizontalScrollBarVisibility="Disabled"
             Style={DynamicResource TextBoxBorderStyle}/>

That should fix the issue. If not, try to reset your resource dictionary by deleting the old style and making a new one by going to that textbox and right-click - Edit Style/Template - Edit a Copy And then select "Resource Dictionary" and choose your resource dictionary

answered on Stack Overflow Apr 19, 2021 by Andrei Zait
1

You should either replace <StaticResource ResourceKey="TextBoxBorderStyle" /> with an actual Style:

<TextBox Text="{Binding MyText}"
         IsReadOnly="{Binding ReadOnly}"
         VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" >
    <TextBox.Resources>
        <Style TargetType="Border" BasedOn="{StaticResource TextBoxBorderStyle}" />
    </TextBox.Resources>
</TextBox>

...or change the TargetType of your Style and simply set the Style property of the TextBox:

<UserControl.Resources>
    <Style TargetType="TextBox" x:Key="TextBoxBorderStyle">
        <Style.Resources>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBoxBase}}" Value="True">
                        <Setter Property="Opacity" Value="0.56"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>
</UserControl.Resources>

Usage:

<TextBox Text="{Binding MyText}"
         IsReadOnly="{Binding ReadOnly}"
         VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled"
         Style="{StaticResource TextBoxBorderStyle}" />
answered on Stack Overflow Apr 19, 2021 by mm8

User contributions licensed under CC BY-SA 3.0