In Universal Windows App flavoured Xaml, I want to create a custom panel that has a very similar attached property such as Canvas.Left (or Top, Right or Bottom). So I can simply create one according to the guide
public static readonly DependencyProperty XProperty =
DependencyProperty.RegisterAttached("X", typeof(double), typeof(Track), null);
public static double GetX(DependencyObject d)
{
return (double)d.GetValue(XProperty);
}
// SetX and property wrapper omitted for brevity here
Now I can write
<c:Track>
<TextBlock Text="1" c:Track.X="1"/>
<TextBlock Text="2" c:Track.X="2"/>
<TextBlock Text="3" c:Track.X="3"/>
</c:Track>
And I can then use my attached values in
public override void ArrangeOverride(Size finalSize)
{
foreach (var child in Children)
{
var x = GetX(child);
child.Arrange(CalculateArrange(x, finalSize));
}
}
And everything is working perfectly so far.
However when we come to an ItemsControl I can do this,
<ItemsControl ItemsSource="{Binding ListOfInts}">
<ItemsControl.ItemPanel>
<ItemPanelTemplate>
<Canvas/>
</ItemPanelTemplate>
</ItemsControl.ItemPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DatatType="Int">
<TextBlock Canvas.Left="{Binding}" Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But if I want to do it for my own custom panel, I have to do this
<ItemsControl ItemsSource="{Binding ListOfInts}">
<ItemsControl.ItemPanel>
<ItemPanelTemplate>
<Track/> <!--Change to my panel-->
</ItemPanelTemplate>
</ItemsControl.ItemPanel>
<ItemsControl.ItemContainerStyle> <!-- need to add the attached property here -->
<Style TargetType="ContentPresenter">
<Setter Property="c:Track.X" Value="{Binding}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate x:DatatType="Int">
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Because I can't do
public override void ArrangeOverride(Size finalSize)
{
//Children are now contentPresenters
foreach (ContentPresenter child in Children)
{
var controlWithAttribute = child.????
//child.Content is the same as DataContext i.e. an Int
var x = GetX(controlWithAttribute);
child.Arrange(CalculateArrange(, finalSize));
}
}
Any ideas what I am missing? How do I get the ItemsControl to work the same as Canvas.Left?