Hi,
I am getting the below error while i am cloning my WPF control using XamlReader.Load method.
XamlReader.Load throws error : Item has already been added. Key in dictionary: 'NewBrushKey' Key being added: 'NewBrushKey'
XAML:
<UserControl x:Class="TestProject.UIControl">
<UserControl.Resources>
<SolidColorBrush x:Key="NewBrushKey" Color="Red" />
</UserControl.Resources>
<Border BorderBrush="{StaticResource NewBrushKey}" BorderThickness="2">
<Grid>
...
...
...
</Grid>
</Border>
</UserControl
//User control is reused in this window.
<Window>
<Grid x:Name="UIControlContainer">
...
...
<ctrl:UIControl x:Name="firstUIControl" />
<ctrl:UIControl x:Name="secondUIControl" />
</Grid>
<Window>
C#:
//UIControlContainer object is closed using XAML Parser in code behind.
string _containerStr = XamlWriter.Save(UIControlContainer);
StringReader _strReader = new StringReader(_containerStr);
XmlTextReader _xmlReader = new XmlTextReader(_strReader);
UIElement _containerClone = (UIElement)XamlReader.Load(_xmlReader);
But XamlReader.Load(_xmlReader) method throws "Item has already been added. Key in dictionary: 'NewBrushKey' Key being added: 'NewBrushKey'"
Please help me how to solve this problem.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.