while parsing one xaml file I get an exception:
Result Message:
Test method ThemeResourceDictionaryTest.ParseXaml threw exception: System.Windows.Markup.XamlParseException: 'Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception.' Line number '1397' and line position '4'. ---> System.ArgumentException: Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox'
and my element:
<Style x:Key="MyVeryUniqueKey" BasedOn="{StaticResource TextBoxStyle}" TargetType="TextBox">
<Setter Property="MinWidth" Value="90"/>
</Style>
Why is the key of the element set to TextBox, when I explicitly set the key to be MyVeryUniqueKey, which is unique.
I have other styles whose target type is TextBox, but they all have different keys.
I suspect that, in a particular resource dictionary scope, you have more than one TextBox
style where the TargetType
is set but the x:Key
is not.
If you don't explicitly set the dictionary key for a TextBox
style it will default to {x:Type TextBox}
. You obviously can't have two of these duplicated as keys in a dictionary.
For example, if you put this kind of thing in resources:
<Window.Resources>
<Style TargetType="{x:Type TextBox}" />
<Style TargetType="{x:Type TextBox}" />
</Window.Resources>
Then you will get a runtime XamlParseException
:
Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox'"