0

i'm trying to add Lines as a ListboxItems to a Canvas. The image contains a Line with startpoint 200,200 and endpoint 600,600. The X of the beginpoint is taken into account, but not the Y ?? (starts at the bottom).

        <Style x:Key="listBoxItemStyle"  TargetType="ListBoxItem">
        <!-- The ListboxItem template has a blue mouseover effect, replaced the template to avoid that -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid Background="{TemplateBinding Background}" MouseDown="zoomAndPanControl_MouseDown" >
                        <!--MouseDown ook hier om begintpunt te kunnen selecteren in een Listboxitem=polyline-->
                        <ContentPresenter
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                Content="{TemplateBinding Content}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Padding}">
                        </ContentPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter 
            Property="Canvas.Left" 
            Value="0.0" 
            />
        <Setter 
            Property="Canvas.Bottom" 
            Value="0.0" 
            />
        <Setter 
            Property="IsSelected" 
            Value="{Binding IsSelected}" 
            />

    </Style>


        <Style x:Key="noScrollViewerListBoxStyle" TargetType="ListBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <Canvas 
                        Background="{TemplateBinding Background}"  IsItemsHost="True"  />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And the datatemplate of my NC_line

        <DataTemplate  DataType="{x:Type models:NC_Line}">
        <Line X1="{Binding X1}" Y1="{Binding Y1}"  X2="{Binding X2}" Y2="{Binding Y2}"  Stroke="red" StrokeThickness="5" Cursor="Hand"/>
    </DataTemplate>

Definition of the drawn Line :

        NC_Line line3 = new NC_Line();

        line3.X1 = 200;
        line3.Y1 = 200;
        line3.X2 = 600;
        line3.Y2 = 600;

What did i forgot ?

enter image description here

Mowag
  • 17
  • 2
  • You may want to use an approach like e.g. shown [here](https://stackoverflow.com/a/40190793/1136211). Do not use a Canvas in the ControlTemplate of a ListBox. Instead, put it into the ItemsPanelTemplate that is used as ItemsPanel. – Clemens Feb 17 '22 at 16:58

0 Answers0