0

我需要使用 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>

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

4

1 回答 1

0

尝试使用ItemsControl而不是 ListBox 并扔掉所有<ListBox.Resources>东西。所有你需要的应该是

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Line ... />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
于 2012-10-29T09:01:31.627 回答