I’m trying to follow along with the code example for data binding to a clr-object.
The example states
<DockPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:SDKSample">
<DockPanel.Resources>
<c:MyData x:Key="myDataSource"/>
</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource myDataSource}"/>
</DockPanel.DataContext>
<Button Background="{Binding Path=ColorName}"
Width="150" Height="30">I am bound to be RED!</Button>
</DockPanel>
However, I seem to be having problems getting a reference the object I created (in C#) from with in my XAML
<Page
x:Class="HelloWindows.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HelloWindows"
xmlns:src="clr-namespace:HelloWindows"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<src:MainPage+Person x:key="person" />
</Page.Resources>
Here is my C#
public Person person = new Person();
public class Person
{
public String name { get; set; }
}
I created the “src” namespace as shown. However, Visual Studio does not recognize “Person” and wants to add “MainPage+Person” in front of it. I get the following error
All objects added to an IDictionary must have a Key attribute or some
other type of key associated with them.
So I am confused about this and also about the “MainPage+Person.” I would assume I need a way to tell the XAML not only the type of object, but also get a handle on the actual object I am creating.
Unfortunately, you’re being challenged by slight differences in the XAML syntax for WPF and for Windows Store applications. The DockPanel example is WPF (DockPanel isn’t a native Windows Store control), and your MainPage would appear to be from a Windows Store app.
Change your namespace declaration from
to
usingis the Windows Store version ofclr-namespace.