使用Listbox WPF在布局中绘制线条

时间:2012-10-29 06:17:48

标签: wpf listbox line

我需要使用WPF的ListBox控件在容器中绘制线条。使用Listbox控件的原因是我有一组对象,这些对象基本上是自定义Line对象,暴露x1,y1和x2,y2点。因此,一旦绘制了一条线,该线将被添加到集合中,以便它可以同时显示。

我用来在列表框中绘制线条的代码是:

<ListBox Grid.Row="0" Grid.Column="0" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.ColumnSpan="4" Grid.RowSpan="4" Name="listBoxLines" Visibility="{Binding ElementName=RadioCaliperOn, Path=IsChecked, Converter={StaticResource booleanToVisibilityConverter}}"  Style="{x:Null}" Background="Transparent" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=Calipers}" >
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Canvas>
                            <!--<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Grid>-->
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <ListBox.Resources>

                        <Style TargetType="ListBoxItem">
                            <Style.Resources>
                                <!-- Background of selected item when focussed -->
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                                <!-- Background of selected item when not focussed -->
                                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                            </Style.Resources>
                            <Style.Setters>
                                <!--<Setter Property="Background" Value="{StaticResource Brush_CZBscanImagePinkBorder}"/>-->
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Foreground" Value="Transparent"/>
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Margin" Value="0"/>
                                <Setter Property="Padding" Value="0" />
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                                <Setter Property="MinHeight" Value="1"/>
                                <Setter Property="MinWidth" Value="1"/>
                                <Setter Property="Height" Value="Auto"/>
                                <Setter Property="Width" Value="Auto"/>
                            </Style.Setters>
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Background" Value="{StaticResource BrushTransparent}"/>
                                    <Setter Property="Foreground" Value="{StaticResource BrushTransparent}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                        <Style TargetType="ListBox">
                            <Style.Setters>
                                <Setter Property="ListBox.BorderThickness" Value="0"></Setter>
                            </Style.Setters>
                        </Style>
                        <Style TargetType="ScrollViewer">
                            <Style.Setters>
                                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"></Setter>
                                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"></Setter>
                            </Style.Setters>
                        </Style>
                    </ListBox.Resources>


                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Line Cursor="Hand" X1="{Binding Converter={StaticResource caliperToLineConverter}, ConverterParameter=X1}" Y1="{Binding Converter={StaticResource caliperToLineConverter}, ConverterParameter=Y1}" X2="{Binding Converter={StaticResource caliperToLineConverter}, ConverterParameter=X2}" Y2="{Binding Converter={StaticResource caliperToLineConverter}, ConverterParameter=Y2}" Stroke="{Binding Converter={StaticResource selectedCalliperColorConverter},RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" StrokeThickness="2" Height="Auto" Margin="0,0,0,0" Stretch="Fill"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>

我面临的问题是,如果集合中有任何现有的行,那么当我只是鼠标点击我的容器绘制一条线时,列表框的现有项目会被选中,反过来我就是无法绘制任何线条。请让我知道我可以轻松绘制线条的确切方法或方法,每条线都是列表框的项目。感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用ItemsControl代替ListBox并丢弃整个<ListBox.Resources>内容。你需要的只是

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Line ... />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>