Reputation: 2963
I have a rather peculiar problem with one of my WPF applications:
I declared a ControlTemplate
as static resource and I can use it inside the Visual Studio Designer - at runtime however, the resource cannot be found.
My WPF code (shortened):
<Window xmlns:PlatformUI="clr-namespace:Microsoft.Internal.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.12.0"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
x:Class="U6656.WebWindow"
... >
<Window.Resources>
<ControlTemplate x:Key="tmplt" x:Name="tmplt" TargetType="Label">
<Label .... >
....
</Label>
</ControlTemplate>
</Window.Resources>
<Grid>
<Label x:Name="lb_test" Template="{StaticResource tmplt}"/>
</Grid>
</Window>
As mentionend - It works perfectly inside the designer and compiles without problem.
However, I get a XAMLParseException
(the underlying exception is a KeyNotFoundException
).
I also tried the following (still without any luck):
Template={...}
-Attribute from my LabelI added the following code after my InitializeComponent()
-call:
lb_test.Template = this.TryFindResource("tmplt") as ControlTemplate;
And now comes the really odd part:
IntelliSense indicates, that the Field this.Resources
is not empty - moreover: it has exactly the resources it should have. Nevertheless - the small line above fails again with a KeyNotFoundException
.
EDIT №1: Here is the Exception dump:
System.Collections.Generic.KeyNotFoundException wurde nicht behandelt.
HResult=-2146232969
Message=Der angegebene Schlüssel war nicht im Wörterbuch angegeben.
Source=PresentationFramework
StackTrace:
bei System.Windows.Baml2006.Baml2006SchemaContext.GetDependencyProperty(Int16 propertyId)
bei System.Windows.Baml2006.Baml2006Reader.Process_PropertyWithExtension()
bei System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord()
bei System.Windows.Baml2006.Baml2006Reader.ReadObject(KeyRecord record)
bei System.Windows.ResourceDictionary.CreateObject(KeyRecord key)
bei System.Windows.ResourceDictionary.OnGettingValue(Object key, Object& value, Boolean& canCache)
bei System.Windows.ResourceDictionary.OnGettingValuePrivate(Object key, Object& value, Boolean& canCache)
bei System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache)
bei System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache)
bei System.Windows.ResourceDictionary.get_Item(Object key)
bei U6656.WebWindow..ctor()
bei U6656.App.InnerStartUp(StartupEventArgs e)
bei U6656.App.OnStartup(StartupEventArgs e)
bei System.Windows.Application.<.ctor>b__1(Object unused)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
bei System.Windows.Threading.DispatcherOperation.InvokeImpl()
bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Windows.Threading.DispatcherOperation.Invoke()
bei System.Windows.Threading.Dispatcher.ProcessQueue()
bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
bei System.Windows.Threading.Dispatcher.Run()
bei System.Windows.Application.RunDispatcher(Object ignore)
bei System.Windows.Application.RunInternal(Window window)
bei System.Windows.Application.Run(Window window)
bei System.Windows.Application.Run()
bei U6656.App.Main()
InnerException:
EDIT №2: When using IntelliSense to observe the value of this.Resources
, I get the following Exception:
XAML-Knotenstream: StartObject fehlt vor StartMember
"System.Windows.Controls.ContentControl.Content".
which means
XAML node stream: StartObject is missing before StartMember
"System.Windows.Controls.ContentControl.Content".
Upvotes: 2
Views: 1591
Reputation: 2963
I solved it myself by using a UserControl instead of templated controls.
Upvotes: 0
Reputation: 675
I had a KeyNotFoundException with a control template defined in generic.xaml
Exception thrown: 'System.Collections.Generic.KeyNotFoundException' in PresentationFramework.dll Exception thrown: 'System.Xaml.XamlObjectWriterException' in System.Xaml.dll Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Switching from a TemplateBinding to Binding RelativeSource={RelativeSource TemplatedParent} resolved the issue.
Upvotes: 1