【问题标题】:Delete from List in WPF C# with Commands使用命令从 WPF C# 中的列表中删除
【发布时间】:2017-11-21 11:32:53
【问题描述】:

我希望您能够单击删除按钮,然后单击屏幕上的目标注释并删除选定的注释,但是目前发生的只是它删除了所有注释,甚至无法选择一个.

按钮

<StackPanel Orientation="Vertical" VerticalAlignment="Center">
                <customControls:ImageButton x:Name="BtnUndo" Height="30" Width="30" ImageSource="..\Images\undo.png" ToolTip="Undo" HorizontalAlignment="Center" VerticalAlignment="Top" Click="BtnUndo_Click" IsEnabled="False" Focusable="False"/>
                <customControls:ImageButton x:Name="BtnRedo" Margin="0,2,0,2" Height="30" Width="30" ImageSource="..\Images\redo.png" ToolTip="Redo" HorizontalAlignment="Center" VerticalAlignment="Top" Click="BtnRedo_Click" IsEnabled="False" Focusable="False"/>
                <customControls:ImageButton x:Name="BtnClear" Height="30" Width="30" ImageSource="..\Images\clear.png" ToolTip="Clear" HorizontalAlignment="Center" VerticalAlignment="Top" Click="BtnClear_Click" IsEnabled="False" Focusable="False"/>
                <customControls:ImageButton x:Name="BtnDeleteTarget" Margin="60,-60,0,2" Height="30" Width="30" ImageSource="..\Images\clear.png" ToolTip="Clear" HorizontalAlignment="Center" VerticalAlignment="Top" Click="DeleteTarget_Click" IsEnabled="True" Focusable="False"/>
            </StackPanel>

事件

private void DeleteTarget_Click(object sender, RoutedEventArgs e)
{
    Common.Commands.CommandManager.Get().Do(new ClearTargetAnnotation(_annotationManager));
}

命令

{
        private AnnotationManager _annotationManager;
        private List<Annotation> _annotations;

        public ClearTargetAnnotation(AnnotationManager annotationManager)
        {
            _annotationManager = annotationManager;
            _annotations = _annotationManager.GetAll();
        }
        public void Do()
        {
            foreach (var annotation in _annotations)
                _annotationManager.Remove(annotation);
        }

        public void Undo()
        {
            _annotationManager.Set(_annotations);
        }
    }
}

删除方法

public void Remove( Annotation annotation )
        {
            Monitor.Enter( _annotations );
            _annotations.Remove( annotation );
            Monitor.Exit( _annotations );

            annotation.OnMoved -= AnnotationMoved;
            Draw();

            if ( _annotations.Count == 0 )
                IsEmptyChanged();
        }

【问题讨论】:

    标签: c# wpf command


    【解决方案1】:

    然而,目前发生的只是它删除了所有注释

      public void Do()
        {
            foreach (var annotation in _annotations)
                _annotationManager.Remove(annotation);
        }
    

    对我来说似乎很合乎逻辑,调用此函数将删除所有注释。您应该有一个从视图中更新的选定注释,并且只从您的注释列表中删除该注释。

    注意:据我所知,您使用的是 WPF,在这种情况下,使用 ObservableCollection 而不是 List 可能会很有用,因为如果您更改它,它将在视图中自动更新。 更多关于 here.

    【讨论】:

    • “您应该有一个从视图更新的选定注释,”选定注释是什么意思,我该怎么做?
    • "我希望你能够点击删除按钮然后点击一个有针对性的注释" 当你点击按钮时,事件被触发,并且所有的注释将被删除,您需要更改逻辑以首先选择注释然后单击按钮,或者您的单击应创建一个标志,以便在单击某些注释时将其排除在外
    • @JsonDork 您应该跟踪在属性中选择了哪个注释,并且只删除它,而不是删除整个列表。
    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2017-07-26
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    相关资源
    最近更新 更多