0

I generate a list of elements with an ObservableList and an ItemsControl. But I need to know the ActualWidth of the single controls with reference to the bound item in the list.

I need the Width to cut down my Polyline so that it is no longer than the Canvas

I tried a OneWayToSource and a TwoWay Binding back to the Item in the List with the Width Property. Always got NaN or 0.

<ItemsControl x:Name="GraphLB" ItemsSource="{Binding}">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <Border Padding="5, 0, 5, 0">
    <StackPanel>
     <TextBlock Text="{Binding Text}" Foreground="{Binding Foreground}"/>
     <Border BorderBrush="{Binding Foreground}" BorderThickness="0.5">
      <Canvas MinHeight="40" MinWidth="100"
       Height="{Binding Height, Mode = TwoWay }"
       Width="{Binding Width, Mode = TwoWay }">
       <Polyline Points="{Binding Line}" Stroke="{Binding Foreground}" StrokeThickness="1"/>
      </Canvas>
     </Border>
    </StackPanel>
   </Border>
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

And the Class for the Bound Objects

class GraphListObject : LabelListObject
{
    public double Height { get; set; }
    public double Width { get; set; }

    private getProperty<PointCollection> getLine;
    public PointCollection Line { get { return GetLine(); } }
    internal getProperty<PointCollection> GetLine { get => getLine; set { getLine = value; } }
}

I expected that the Width="{Binding Width, Mode = TwoWay }" would set the Value in the Element of the List to the Width of the Canvas Element, but the actual output is 0.

Sadly the Polyline is drawn outside the borders:

enter image description here

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • 2
    The Canvas Width and Height are not set by layout. It's ActualWidth and ActualHeight instead. However, you can't bind these properties because they are readonly. See this for a workaround: https://stackoverflow.com/q/1083224/1136211. That said, doing this seems not to be necessary at all. The parts of the Polyline outside the Border's bounds aren't drawn anyway. – Clemens Nov 12 '19 at 10:10
  • You mean outside the bounds of the inner Border? By the way, why do you have the outer Border at all? Seems redundant. – Clemens Nov 12 '19 at 11:04
  • Sadly they are drawn https://prnt.sc/pvu6gj <- Screenshot The outer Border is only for the Padding – MEGACoolBlood Nov 12 '19 at 11:09
  • Sorry, I won't follow external links. Outside the bounds of the inner Border or not? – Clemens Nov 12 '19 at 11:10
  • For the Padding, you might as well set the Margin of the StackPanel. – Clemens Nov 12 '19 at 11:15
  • But thats not the Problem. I just need to know how far i can draw the line. – MEGACoolBlood Nov 12 '19 at 11:16
  • Do you want to scale your line to be always within the canvas or do you want to ensure that the line doesn't appear outside the canvas? In second case, `ClipToBounds="true"` might help – grek40 Nov 12 '19 at 12:07
  • That would fix a part of the problem, but I also want to cut the line at the beginning, so that the graph show the latest Information, this method cut on the wrong side. – MEGACoolBlood Nov 13 '19 at 08:39
  • `HorizontalAlignment="Right"`? – grek40 Nov 14 '19 at 18:37

0 Answers0