ObservableCollection계열의 Lines 객체 추가시 문제 문의

안녕하세요.

작업하다 생각지도 못한 문제가 발생해서요…

문제상황

  1. TrulyObservableCollection Lines라는 컬랙션 객체안에
    PointCollection이 있습니다. 그래서 PointCollection 객체 하나 만으로 선을 만들 수 있습니다.

  2. 그런데, 문제는 Poly라인이 따로 따로 만들어져야되는데 Poly라인의 끝과 끝이 서로 연결되는 문제가 있습니다.

제가 원하는 방식은 아래 이미지와 같습니다.

문제가 발생한 상황

Cap 2022-05-04 17-00-29-373

CanvasGroupView.xaml

    <ItemsControl ItemsSource="{Binding Lines, UpdateSourceTrigger=PropertyChanged}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Canvas>
                    <Polyline Points="{Binding LinePoints,UpdateSourceTrigger=PropertyChanged}"
                              StrokeThickness="15.0"
                              Stroke="LawnGreen" />

                    <ItemsControl ItemsSource="{Binding LinePoints, UpdateSourceTrigger=PropertyChanged}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Canvas />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>

                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Path StrokeThickness="2.0"
                                      Stroke="Black"
                                      Fill="LawnGreen">
                                    <Path.Data>
                                        <EllipseGeometry Center="{Binding}"
                                                         RadiusX="8"
                                                         RadiusY="8" />
                                    </Path.Data>
                                </Path>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Canvas>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

LinePointViewModel.cs

public class LinePointViewModel
        : INotifyPropertyChanged
    {
        public LinePointViewModel()
        {
            _linePoints = new PointCollection();
        }
        private PointCollection _linePoints;

        public event PropertyChangedEventHandler PropertyChanged;

        public PointCollection LinePoints
        {
            get { return _linePoints; }
            set
            {
                _linePoints = value;
                RaisePropertyChanged("LinePoints");
            }
        }
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

CanvasGroupViewModel.cs

public class CanvasGroupViewModel
    : CanvasEntityViewModel<SymbolGroupProvider>
    {
      
        public CanvasGroupViewModel(SymbolGroupProvider entityProvider, bool visibility = true) : base(entityProvider, visibility)
        {
            Lines = new TrulyObservableCollection<LinePointViewModel>();
            _groupProvider = IoC.Get<GroupProvider>();
        }
 
         public void CreateLines(int mapNumber)
        {
            try
            {
             
                Lines.Clear();
                var mappedGroups = _groupProvider.Where(t => t.Map == mapNumber);

                var pointCollection = new PointCollection();


                foreach (var group in mappedGroups)
                {
                    foreach (var entity in EntityProvider)
                    {
                        if(entity.NameArea == group.NameArea)
                            pointCollection.Add(new Point(entity.X1, entity.Y1));
                    }

                    if (pointCollection.Count() > 0)
                    {
                        var line = new LinePointViewModel();
                        line.LinePoints = pointCollection;
                        Lines.Add(line);
                    }
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception Rasied in CreateLines : {ex.Message}");
            }
        }
      
        public TrulyObservableCollection<LinePointViewModel> Lines
        {
            get { return _lines; }
            set
            {
                _lines = value;
                NotifyOfPropertyChange(()=>Lines);
            }
        }

 
        private GroupProvider _groupProvider;
        private TrulyObservableCollection<LinePointViewModel> _lines;
     

    }
}

제가 예상하기로는 Xaml쪽만 수정하면 될 것같은데,

어떻게 접근하면 좋을까요??

1개의 좋아요

제 기억에 아마 Polyline 때문인 것 같아요.
이전에 비슷한 일을 겪었던 기억이 있어서 맨 바깥에 Path 객체만 두고 내부에 PathSegment를 이용해서 해결했었습니다.

4개의 좋아요

네 저도 그 레퍼런스 찾아서 해보는데 잘 안되는거 같아서 더 시도해보겠습니다~ 감사합니다!!^^

1개의 좋아요