0

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?

AlSki
  • 6,868
  • 1
  • 26
  • 39
  • `Canvas.Left="{Binding X}"` for an item in an ItemsControl with a Canvas as ItemsPanel would also have to be set in an ItemContainerStyle, not in an ItemTemplate. However, Bindings in Style Setters are not supported in WinRT/UWP. There are workarounds available, search SO. – Clemens Dec 04 '15 at 10:19
  • See [this answer](http://stackoverflow.com/a/33582406/1136211). – Clemens Dec 04 '15 at 11:05
  • Thanks Clemens. No I'll revise that, SetBinding in the PropertyChangedEventHandler is Beautiful ;-) – AlSki Dec 04 '15 at 11:10
  • P.S. If you add this as an answer I'll accept it. – AlSki Dec 04 '15 at 11:18
  • @Clemens: Are you sure this is duplicate? Maybe binding are not workign in styles, but he is asking someting else, imho – Liero Dec 04 '15 at 11:47
  • @Liero I am. The question was why an attached property won't work like `Canvas.Left`. The answer is, it does work exactly the same way, but it requires a workaround for the unavailable Style Setter Binding (which would also be necessary for a `Canvas.Left` binding). – Clemens Dec 04 '15 at 12:01
  • @Clemens: ok, if you replace the binding in style with constant value, the question still remains valid, doesn't it? I believe his question is not related to binding, but to the `????` in his last code block. It has nothing to do with bindings although he might incorrectly use bidning in style – Liero Dec 04 '15 at 12:26
  • My view is this, I was asking a question based on the wrong understanding, i.e.that you can normally use Canvas.Left without applying it to the ContentPresenter, which is wrong. However I was also given extra information to get around a problem I hadn't had yet but was about to, which is that I can't apply a Binding in the style, To me, this doesn't feel like a duplicate of the linked question, but possibly is of another question. If the original scenario is useful to other people and not duplicated then keep it, else close it. – AlSki Dec 04 '15 at 23:42

0 Answers0