复杂的数据绑定问题

时间:2014-11-09 15:57:18

标签: c# wpf polyline

我正在尝试开发数据绑定折线画布。 目前,我对一个类列表进行了数据绑定,每个类都包含一个pointcollection和一个偏移量。但是我想进一步定制这些线条,例如它们的颜色。因此,我想在包含折线模型类的类中创建一个List。其中包含一个点集,偏移X,Y和折线颜色的数据。

目前它看起来像这样:

                <Canvas Name="PolyLineCanvas" Width ="100" Height="100">
                    <ItemsControl ItemsSource="{Binding WaypointList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Canvas/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="ContentPresenter">
                                <Setter Property="Canvas.Left" Value="{Binding Path=TracedPathLeft, UpdateSourceTrigger=PropertyChanged}"/>
                                <Setter Property="Canvas.Top" Value="{Binding Path=TracedPathTop, UpdateSourceTrigger=PropertyChanged}"/>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Polyline Stroke="Magenta" Points="{Binding TracedPath}" StrokeThickness="0.0244"></Polyline>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Canvas>

上课:

public class WaypointListModel : INotifyPropertyChanged
{

    public class Waypoint : INotifyPropertyChanged
    {
        ObservableCollection<Waypoint> wptList;

        PointCollection tracedPath;
        double tracedPathTop;
        double tracedPathLeft;

        public Waypoint(ObservableCollection<Waypoint> WptList)
        {
            wptList = WptList;
        }

        public PointCollection TracedPath
        {
            get
            {
                return tracedPath;
            }
            set
            {
                tracedPath = value;
            }
        }

        public double TracedPathTop
        {
            get
            {
                return tracedPathTop;
            }
            set
            {
                tracedPathTop = value;
            }
        }

        public double TracedPathLeft
        {
            get
            {
                return tracedPathLeft;
            }
            set
            {
                tracedPathLeft = value;
            }
        }

        public void NotifyCanvas() //Method being called when pressing an update button
        {
            NotifyPropertyChanged("TracedPathTop");
            NotifyPropertyChanged("TracedPathLeft");
            NotifyPropertyChanged("TracedPath");
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    ObservableCollection<Waypoint> waypointList = new ObservableCollection<Waypoint>();

    public ObservableCollection<Waypoint> WaypointList
    {
        get
        {
            return waypointList;
        }
        set
        {
            waypointList = value;

            NotifyPropertyChanged("WaypointList");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

这是我制作的polylineModel类(类似于当前向上的类):

class WayPointPolyline : INotifyPropertyChanged
{
    SolidColorBrush tracedPathColour;
    PointCollection tracedPath;
    double tracedPathTop;
    double tracedPathLeft;

    public SolidColorBrush TracedPathColour
    {
        get
        {
            return tracedPathColour;
        }
        set
        {
            tracedPathColour = value;
            NotifyPropertyChanged("TracedPathColour");
        }
    }

    public PointCollection TracedPath
    {
        get
        {
            return tracedPath;
        }
        set
        {
            tracedPath = value;
            NotifyPropertyChanged("TracedPath");
        }
    }

    public double TracedPathTop
    {
        get
        {
            return tracedPathTop;
        }
        set
        {
            tracedPathTop = value;
            NotifyPropertyChanged("TracedPathTop");
        }
    }

    public double TracedPathLeft
    {
        get
        {
            return tracedPathLeft;
        }
        set
        {
            tracedPathLeft = value;
            NotifyPropertyChanged("TracedPathLeft");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

}

然而在名为Waypoint的课程中

我想用以下代码替换PointCollection:

        public List<WayPointPolyline > TracedPaths
        {
            get
            {
                return tracedPaths;
            }
            set
            {
                tracedPaths = value;
            }
        }

xaml是硬件出现的地方,因为目前它只在List中添加了每个类的一个pointcollection,我需要的是一个List,你可以在其中添加x类,类x可以包含y行的数据。知道该怎么办吗?

先谢谢!

1 个答案:

答案 0 :(得分:0)

向Chris Eelmaa致信提示!

Xaml应该是这样的:我没有注意到性能上有任何显着差异。然而,作为一个amatuer,知道下面的代码是否过于混乱&#34;

会很有趣。
                <Canvas Name="PolyLineCanvas" Width ="100" Height="100">
                    <ItemsControl ItemsSource="{Binding WaypointList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Canvas/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <ItemsControl ItemsSource="{Binding TracedPathList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <Canvas/>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.ItemContainerStyle>
                                            <Style TargetType="ContentPresenter">
                                                <Setter Property="Canvas.Left" Value="{Binding Path=TracedPathLeft, UpdateSourceTrigger=PropertyChanged}"/>
                                                <Setter Property="Canvas.Top" Value="{Binding Path=TracedPathTop, UpdateSourceTrigger=PropertyChanged}"/>
                                            </Style>
                                        </ItemsControl.ItemContainerStyle>
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                            <Polyline StrokeDashArray="{Binding TracedPathDashStyle}" Stroke="{Binding TracedPathColour}" Points="{Binding TracedPath}" StrokeThickness="0.0244"></Polyline>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Canvas>