I am developing Windows 10 Universal App. I have code below:
xaml:
<Page
x:Class="MyProject.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Purple"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Red" Width="50" Height="50"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Page>
and code behind:
namespace MyProject
{
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
DataContext =
new[]
{
new { X = 50.0, Y = 100.0 },
new { X = 220.0, Y = 170.0 }
};
InitializeComponent();
}
}
}
Unfortunatelly, the rectangles does not show in the window. I am getting compilation error:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Assigning to Canvas coordinates static numbers in xaml works as expected.
Why error occurs and code does not work?
ItemsSource
binding to your control? If so, have you tried using a named type instead of an anonymous type? E.g.Tuple<double, double>
or your own user-defined type? Have you tried seeing theCanvas.Left
andCanvas.Top
properties in theItemTemplate
instead of the content presenter's style? – Peter DunihoItemTemplate
properties. What do you mean by usingTuple<double, double>
? – pt12lolValue
in Winrt apps. It works in WPF, but not in Winrt. You may want to consider usingRectangle.RenderTransform
to position your rectangles instead. – Peter Duniho