无法在ItemsControl WPF中更改z索引

时间:2019-06-11 12:24:34

标签: wpf z-index itemscontrol

当我单击它们时,我想动态更改线条的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索引被更新,并且轨道实质上被重新渲染为位于轨道旁边,因此根本没有灰色覆盖红色。通过某种方式更改我的代码是否可能?感谢您的任何提前帮助。

0 个答案:

没有答案