WPF ResourceDictionary Key Property "Key" was not found - wpf

I created a DateTimeConverter to convert my datetimes to MM/dd/yyyy format. I tried using it in my xaml file as:
<Window.Resources>
<converters:DateTimeConverter Key="DateTimeConverter"/>
</Window.Resources>
and it caused an error saying: Property Key was not found in type DateTimeConverter.
Then after a bit of googling, I changed it to:
<Window.Resources>
<converters:DateTimeConverter x:Key="DateTimeConverter"/>
</Window.Resources>
and it worked (note the "x:" before Key).
My "x" is defined by default as: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
It is working now, but I don't know why. Can someone please throw some light?

Adding an x:Key value to a XAML object element is the most common way to identify a resource in a resource dictionary.
It's the syntax chosen for WPF xaml. Key property or attribute doesn't exist.
Refer to MSDN specs for more clarification.

Related

LayoutRounding property is not defined

I have taken a copy of the XamlVirtualJoystick project located here
https://github.com/shakram02/XamlVirtualJoystick
I wanted to convert it from a UserControl to a custom control, with the XAML code in a resource dictionary. (I don't know if that is relevant to the my error.)
In my project, the identical XAML generates the error message:
The property 'LayoutRounding' does not exist in XML namespace 'http://schemas.microsoft.com/expression/blend/2008'.
This refers to the property d:LayoutRounding="Auto", where the prefix d is defined as
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
I can compile the original project, so the property must be defined somewhere, but I cannot figure out where.
Where is the property LayoutRounding defined?
do you have mc:Ignorable="d" in your xaml defined? d is usually the designer name space and mc:Ignorable="d" says that all properties with prefix d: are to be ignored by the compliler and are only used in blend designer view.

What does the <vm:SimpleViewModel x:Key="viewModel"/> mean in WPF?

I am new to WPF and MVVM, actually started just a week back and I am trying to code up an application using both WPF and MVVM, while coding up an example I came across the following statement <vm:SimpleViewModel x:Key="viewModel"/> and I am trying to reason about it. I understand what 'x:' refers to, its the default XAML namespace mentioned in the XAML file and I have created a namespace for my own ViewModel class that the UI will be interacting with and I have given it an alias "vm" and SimpleViewModel is the ViewModel for my application, the statement for the purposes of reference is xmlns:vm="clr-namespace:MVVM_Tutorial".
My Reasoning for the statement <vm:SimpleViewModel x:Key="viewModel"/> is that a Window is a XAML element and has a resource dictionary that it refers to resolve and refer to certain elements, hence inside its resource dictionary which is defined in the "x:" namespace we are assigning a variable called "Key" whose value is the SimpleViewModel class defined in the "vm:" namespace. Just want to know if I am right with my reasoning or is there something that I am missing and would want to know proceeding further from here.
XAML is just markup that describes an object graph. Code is also markup that describes an object graph. I can say this
var window = new Window();
window.DataContext = new MyNamespace.MyViewModel();
or I can write the exact same thing like this
<Window xmlns:blahblah="clr-namespace:Normal.Xmlns.Deleted.For.Brevity"
xmlns:this="clr-namespace:MyNamespace">
<Window.DataContext>
<this:MyViewModel />
<!-- snip -->
Any object that can be instantiated in code can be used in xaml. There are some restrictions (e.g., default public constructor without arguments), but for the most part this is true. XAML just defines an object graph that is deserialized at runtime.
Since any type can be referred to in xaml, you could, hypothetically, have moved that instance of MyViewModel to a resource dictionary and referred to it via a StaticResource or a DynamicResource. Note, anything you put in a resource dictionary has to have a key, assigned via x:Key:
<Window xmlns:blahblah="clr-namespace:Normal.Xmlns.Deleted.For.Brevity"
xmlns:this="clr-namespace:MyNamespace"
DataContext="{DynamicResource lolderp}">
<Window.Resources>
<this:MyViewModel x:Key="lolderp" />
<!-- snip -->
XAML is a subset of XML, and uses XML namespaces to map to code namespaces in the current, or other, assemblies. It's how the framework knows what object MyViewModel refers to. To learn more, read this link on msdn.
I'm sure someone else can chime in with more clarification...
In the xaml file, the references of
"xmlns:[something]="clr-namespace:[yourProjectOrLibrary]".
Since your code-behind can be verbose with long name space references, and your SOLUTION may be made up of multiple projects (such as different DLLs), when the XAML is processed, it uses the "xmlns" as a reference to whatever "yourProjectOrLibrary" is... In your case the project/class "MVVM_Tutorial".
Now, the "vm". This is just an "alias" within the xaml, so anytime it is referencing a
The xaml knows where it originates to get resolution to control, properties, types, etc.
As for the "x:Key" part... Not positive, but when I was first building out my customized themes, also ran into confusion about the x:Key. My interpretation of this was found to be x:Key is like a private reference, but by being given the name ..x:Key="viewModel"... is making this "name" available later within the xaml file.
This "key" can then be referenced later in the xaml... For example,
<ControlTemplate x:Key="CTButton" TargetType="{x:Type Button}" >
<!-- Start border of button to have a rounded corners -->
</ControlTemplate>
Then later within the theme, I could reference this "Key"... in my case "CTButton". So if I wanted multiple controls to use / derive from same control template, I could have them reference it...
<someControl>
<Style>
<Setter Property="Template" Value="{StaticResource CTButton}" />
</Style>
</someControl
Again, I don't get EVERYTHING about all the xaml markup, but hopefully clarifies this for you some.

Trouble adding a DataTemplateSelector to a wpf XAML

This is my first time ever doing anything with XAML and I'm a bit lost.
I'm trying to add a DataTemplateSelector to my XAML page using the instructions found here.
However I'm stuck on the part where I'm supposed to "declare the TaskListDataTemplateSelector as a resource". This is the example given:
<Window.Resources>
...
<local:TaskListDataTemplateSelector x:Key="myDataTemplateSelector"/>
...
</Window.Resources>
However, the top level tag in the XAML document is <UserControl>, and I don't see anything about Window or Window.Resources.
Under the <UserControl> tag is a <UserControl.Resources> tag, but when I try to put the example code there, it gives me an error saying "The namespace prefix 'local' is not defined". I tried replacing "local" with the actual namespace, but got the same error.
The custom DataTemplateSelector class is in the codebehind for the XAML page, and is in the same namespace.
For anyone who googled this question like I did:
I think I found the answer.
You need to define the local prefix in your XAML file.
This can be done by placing the following namespace argument in the root element:
xmlns:local="clr-namespace:AssemblyName"
Change "AssemblyName" in your erm.. assembly's name.

ContextMenu static resource compile error

I've created a Silverlight control with a ContextMenu property. I've also created a ContextMenu static resource, which one I want to add to the control, but I get a compile error.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" >
<controlsInputToolkit:ContextMenuService.ContextMenu x:Key="FilterableTreeViewContextMenu">
<controlsInputToolkit:ContextMenu>
<controlsInputToolkit:MenuItem Header="New" />
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
</ResourceDictionary>
The error:
Unexpected ATTRIBUTE in parse rule PropertyElement ::= . PROPERTYELEMENT Content? ENDTAG..
Any idea?
This error indicates that an attribute has been applied to a property element which is not valid. In your case, this is the x:Key attribute on the controlsInputToolkit:ContextMenuService.ContextMenu element. You will need to remove it.
You can identify property elements in XAML by the period that is included within the name of the element. The part of the name prior to the period is the name of a class and the part that follows is a property on that class. Attributes are not allowed within such elements.
Your XAML was compact enough for me to easily find the problem. If this error occurs within a much larger XAML file, then the following regular expression can be used within Visual Studio to locate the problem. Just make sure to check the 'Use' checkbox in the 'Find and Replace' dialog and select 'Regular Expressions' in the combo box.
\<:Al#.:Al# :Al#=\".#\":b#>
If you need to adjust the expression then the MSDN documentation on regular expression syntax will be useful.

Why are XML DataTemplates ignored?

In the following sample, the DataTemplate is ignored by WPF.
Why is this?
<Window x:Class="TestXmlNonBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Xml="clr-namespace:System.Xml;assembly=System.Xml">
<Window.Resources>
<DataTemplate DataType="{x:Type Xml:XmlDocument}">
<TextBlock>Hello</TextBlock>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<Xml:XmlDocument></Xml:XmlDocument>
</Window.DataContext>
<Grid>
<ContentControl Content="{Binding}"></ContentControl>
</Grid>
I believe the issue is with binding, not template selection.
If you look at the documentation for Binding.XPath, you'll see that when the binding source is XML data (i.e. an XmlDocument or XmlNode) the XPath property is used, rather than the Path property, to find the property on the data source.
I suspect that what's happening here is that the Binding is not returning an XmlDocument. The binding sees that the source object is an XmlDocument, and it calls SelectNodes on it, passing in the value of the XPath property as an argument. That's null (or maybe an empty string), and so SelectNodes doesn't return anything.
DataTemplates have a special functionality to deal with XML, if there is XML-data the DataType is interpreted as the name of the XML-element that should be templated:
If the template is intended for object data, this property contains the type name of the data object (as a string). To refer to the type name of the class, use the x:Type Markup Extension. If the template is intended for XML data, this property contains the XML element name. See the documentation remarks for details about specifying a non-default namespace for the XML element.

Resources