【问题标题】:Draw lines in layout using Listbox WPF使用 Listbox WPF 在布局中绘制线条
【发布时间】:2012-10-29 06:17:47
【问题描述】:

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

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

【问题讨论】:

  • 我不太明白你在做什么,你能解释一下吗?

标签: wpf listbox line


【解决方案1】:

尝试使用 ItemsControl 而不是 ListBox 并扔掉整个 &lt;ListBox.Resources&gt; 的东西。所有你需要的应该是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2013-07-14
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多