Невозможно изменить z-индекс в ItemsControl WPF - PullRequest
0 голосов
/ 11 июня 2019

Я хочу динамически изменять z-индекс моих строк, когда нажимаю на них, но пока не добился успеха.

Вот мой XAML, где я просто привязываюсь к списку моделей представлений дорожек, а затем к списку координат линий в каждой модели представлений:

<ItemsControl ItemsSource="{Binding CircuitTracks}">

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- Bind onto List of LineCoords in each CircuitTracks object -->
            <ItemsControl ItemsSource="{Binding LineCoords}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas ZIndex="{Binding ZIndex}"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <!-- For each LineCoord object, draw out a line with coordinates. Currently bound to LineCoord object. 1 Ancestor level up is List of Line Coords, 2 Levels up is LDLTrack Object -->
                    <DataTemplate>
                        <Canvas>
                            <Line  X1="{Binding X1, Converter={StaticResource ScaleXCoordConverter}}" Y1="{Binding Y1, Converter={StaticResource ScaleYCoordConverter}}"
                                   X2="{Binding X2, Converter={StaticResource ScaleXCoordConverter}}" Y2="{Binding Y2, Converter={StaticResource ScaleYCoordConverter}}"
                                   Stroke="{Binding DataContext.LineColor, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" 
                                   StrokeThickness="2" StrokeEndLineCap="Flat" StrokeStartLineCap="Flat">
                                <Line.InputBindings>
                                    <MouseBinding Gesture="LeftClick" Command="{Binding DataContext.OccupyTrackCommand, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}"/>
                                </Line.InputBindings>

                            </Line>

                        </Canvas>
                    </DataTemplate>

                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Panel.ZIndex" Value="{Binding ZIndex}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>

            </ItemsControl>


        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

https://imgur.com/a/gOvff5H

Как вы можете видеть здесь, когда я щелкаю трек, трек в настоящий момент становится красным с помощью команды. В рамках команды я также изменил целочисленное значение ZIndex на более высокое значение, и это свойство привязано к моему XAML.

Вот мой код модели вида:

public class CircuitTrackViewModel : BaseViewModel {

        public CircuitTrackViewModel(TrackCircuit trackCircuit) {
            TrackCircuit = trackCircuit;
            LineCoords = new ObservableCollection<LineCoordinates>();
            ConvertMSection();

        }

        private TrackCircuit _trackCircuit;

        public TrackCircuit TrackCircuit {
            get => _trackCircuit;
            set {
                _trackCircuit = value;
                OnPropertyChanged("TrackCircuit");
            }

        }
        private ICommand _occupyTrackCommand;
        public ICommand OccupyTrackCommand => _occupyTrackCommand =
            _occupyTrackCommand ?? new DelegateCommand(ChangeColor);

        private string _lineColor = "DimGray";
        public string LineColor {
            get => _lineColor;
            set {
                if (_lineColor == value)
                    return;
                _lineColor = value;
                OnPropertyChanged("LineColor");
            }
        }

        private int _zIndex;

        public int ZIndex {
            get => _zIndex;
            set {
                if (_zIndex == value)
                    return;
                _zIndex = value;
                OnPropertyChanged("ZIndex");
            }
        }

        public void ChangeColor() {
            LineColor = LineColor == "DimGray" ? "Red" : "DimGray";
            ZIndex = ZIndex == 0 ? 100 : 0;
        }

        public ObservableCollection<LineCoordinates> LineCoords { get; set; }

Я хочу, чтобы при нажатии дорожки индекс Z обновлялся, и дорожка по существу перерисовывалась, чтобы располагаться на вершине дорожки рядом с ней, поэтому серый цвет вообще не закрывал красный. Возможно ли это изменить каким-либо образом мой код? Спасибо за любую помощь заранее.

...