WPF:同时显示折线和点

发布于 2024-11-06 05:14:45 字数 1505 浏览 0 评论 0原文

我有一个线段列表,每个线段都包含一个点列表。由于包含在同一画布上,我想显示每个线段并同时标记每个点位置(即带有椭圆)。我可以使用 ItemsControl 来显示段,但我不知道如何显示点。我开始实现一个从 Shape 派生的自定义控件,但必须有一种更简单的方法。预先感谢您的帮助。

public class VesselAnatomy : IEnumerable, INotifyCollectionChanged
{
...
List<BaseVessel> _Segments;
...
}

public class BaseVessel : INotifyPropertyChanged
{
...
ObservableCollection<Point> _VesselPoints;
public ObservableCollection<Point> VesselPoints
        {
            get
            {
                return _VesselPoints;
            }
        }
...
}

public MainWindow()
{
...
VesselAnatomy Vessels = new VesselAnatomy();
...
MasterContainer.DataContext =  Vessels;
...
}

<ItemsControl x:Name="VesselDisplay"
                          Height="750"
                          Width="750"
                      ItemsSource="{Binding}">
                        <Polyline Points="{Binding VesselPoints, Converter={StaticResource ObsListPointConverter}}"
                            Stroke="Red"
                            StrokeThickness="7">
                            <Polyline.ToolTip>
                                <ToolTip>
                                    <TextBlock Text="{Binding Name}"/>
                                </ToolTip>
                            </Polyline.ToolTip>
                        </Polyline>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

I have a list of line segments and each line segment contains a list of points. Being contained on the same canvas, I want to display each line segment and simultaneously mark each point location (ie w/ an ellipse). I can use an ItemsControl to display the segments but I'm stuck at how to display the points. I began implementing a custom control derived from Shape, but there must be an easier way. Thanks in advance for the help.

public class VesselAnatomy : IEnumerable, INotifyCollectionChanged
{
...
List<BaseVessel> _Segments;
...
}

public class BaseVessel : INotifyPropertyChanged
{
...
ObservableCollection<Point> _VesselPoints;
public ObservableCollection<Point> VesselPoints
        {
            get
            {
                return _VesselPoints;
            }
        }
...
}

public MainWindow()
{
...
VesselAnatomy Vessels = new VesselAnatomy();
...
MasterContainer.DataContext =  Vessels;
...
}

<ItemsControl x:Name="VesselDisplay"
                          Height="750"
                          Width="750"
                      ItemsSource="{Binding}">
                        <Polyline Points="{Binding VesselPoints, Converter={StaticResource ObsListPointConverter}}"
                            Stroke="Red"
                            StrokeThickness="7">
                            <Polyline.ToolTip>
                                <ToolTip>
                                    <TextBlock Text="{Binding Name}"/>
                                </ToolTip>
                            </Polyline.ToolTip>
                        </Polyline>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-11-13 05:14:45

您也可以将 ItemsControl 用于点,只需更改 ItemsPanel 并绑定元素位置即可。

<Window ... >
    <Window.Resources>
        <PointCollection x:Key="points">
            <Point X="20" Y="20" />
            <Point X="40" Y="35" />
            <Point X="60" Y="40" />
            <Point X="80" Y="60" />
            <Point X="100" Y="40" />
            <Point X="120" Y="30" />
            <Point X="140" Y="40" />
            <Point X="160" Y="20" />
        </PointCollection>
        <DataTemplate DataType="{x:Type Point}">
            <Ellipse Width="9" Height="9" Fill="White" Stroke="DodgerBlue" StrokeThickness="1" x:Name="e">
                <Ellipse.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="-4" Y="-4" />
                        <TranslateTransform X="{Binding X}" Y="{Binding Y}" />
                    </TransformGroup>
                </Ellipse.RenderTransform>
            </Ellipse>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Polyline x:Name="line" Stroke="LightBlue" StrokeThickness="2" Points="{StaticResource points}" /> 
        <ItemsControl x:Name="ptsdisplay" ItemsSource="{StaticResource points}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Window>

如果您有很多点并且此方法太慢,请尝试 http://msdn。 microsoft.com/en-us/magazine/dd483292.aspx

You can use an ItemsControl for the points aswell just change the ItemsPanel and bind the elements positions.

<Window ... >
    <Window.Resources>
        <PointCollection x:Key="points">
            <Point X="20" Y="20" />
            <Point X="40" Y="35" />
            <Point X="60" Y="40" />
            <Point X="80" Y="60" />
            <Point X="100" Y="40" />
            <Point X="120" Y="30" />
            <Point X="140" Y="40" />
            <Point X="160" Y="20" />
        </PointCollection>
        <DataTemplate DataType="{x:Type Point}">
            <Ellipse Width="9" Height="9" Fill="White" Stroke="DodgerBlue" StrokeThickness="1" x:Name="e">
                <Ellipse.RenderTransform>
                    <TransformGroup>
                        <TranslateTransform X="-4" Y="-4" />
                        <TranslateTransform X="{Binding X}" Y="{Binding Y}" />
                    </TransformGroup>
                </Ellipse.RenderTransform>
            </Ellipse>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Polyline x:Name="line" Stroke="LightBlue" StrokeThickness="2" Points="{StaticResource points}" /> 
        <ItemsControl x:Name="ptsdisplay" ItemsSource="{StaticResource points}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Window>

If you have lots of points and this method is too slow try http://msdn.microsoft.com/en-us/magazine/dd483292.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文