Caliburn.Micro-使用依赖注入时从列表框中删除项目

时间:2019-07-19 20:12:24

标签: c# wpf xaml mvvm caliburn.micro

我正在尝试从通过AddButton填充的ListBox中删除一个项目,每个项目都包含其删除按钮,我在Caliburn.micro之外尝试了解决方案,但似乎没有一个适合我, 我将与您分享我上一次尝试的尝试:

这是我的ViewModel代码:

public POSViewModel()
        {

            DeleteTicket= new RelayCommand
                    (() => DeleteTicketCommand(),
                     () => CanDeleteTicket);
        }

  public RelayCommand DeleteTicket { get; private set; }

 public void DeleteTicketCommand()
        {
            Tickets.Remove(SelectedTicket);
            NotifyOfPropertyChange(() => Tickets);
            NotifyOfPropertyChange(() => Total);
        }

        public bool CanDeleteTicket {
            get
            {
                bool output = false;
                if (SelectedTicket != null)
                {
                    output = true;
                }
                return output;
            }
        }



        private TicketModel _selectedTicket;

        public TicketModel SelectedTicket
        {
            get { return _selectedTicket; }
            set {
                _selectedTicket = value;
                NotifyOfPropertyChange(() => SelectedTicket);

            }
        }

这是我的Xaml代码:

<ListBox  HorizontalAlignment="Left"  x:Name="Tickets" SelectedItem="{Binding SelectedTicket}">

<ListBox.ItemTemplate>
     <DataTemplate >
             <StackPanel>
                 <StackPanel Orientation="Vertical" CanVerticallyScroll="False">
                    <Button  Margin="5" 
                      x:Name="DeleteTicket" />
                        <Image Source="{Binding ImageSource }" Stretch="None" HorizontalAlignment="Left" Width="100" VerticalAlignment="Center"/>
                                        </StackPanel>
                                       </StackPanel>
     </DataTemplate>
</ListBox.ItemTemplate>

我忘了提起我尝试使用列表框之外的按钮,它的效果很好,但这不是我想要的,我可以接受任何建议。谢谢!

0 个答案:

没有答案