执行删除命令后,视图未更新

时间:2016-05-23 06:20:36

标签: c# wpf mvvm command

我正在使用中继命令 删除 来自list-item的一些数据, 我有网站列表,我正在使用 observable collection 来获取自动通知。

public SiteMainUC_VM()
    {
        deleteSiteCommand = new RelayCommand(this.DeleteSite);
        selectionChangedCommand = new RelayCommand(this.FilterSite);

        List<SiteDetails> parameters = MasterLink.Model.DALService.ConfigEngine.GetAllSites();
        listofsites = new ObservableCollection<SiteDetails>(parameters);

    }
    private ICommand deleteSiteCommand;
    public ICommand DeleteSiteCommand
    {
        get
        {
            return deleteSiteCommand;
        }
        set
        {
            value = deleteSiteCommand;
        }
    }


    public void DeleteSite()
    {

        var Result = MessageBox.Show("Do You Need to Delete Site Details", "Confirmation", MessageBoxButton.YesNo);
        if (Result == MessageBoxResult.Yes)
        {
            try
            {
               MasterLink.Model.DALService.ConfigEngine.RemoveSite(Selectedsites);
                List<SiteDetails> parameters = MasterLink.Model.DALService.ConfigEngine.GetAllSites();
                listofsites = new ObservableCollection<SiteDetails>(parameters);
                //CollectionViewSource.GetDefaultView(Listofsites).Refresh();
            }
            catch
            {
                MessageBox.Show("Error: Site Not Found !");
            }

        }


    }

现在在 xaml 内,我点击删除功能工作正常但我使用图片作为删除按钮列表框你没有更新, 绑定代码在

之下
<Button Canvas.Right="0" Canvas.Top="5" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=DataContext.DeleteSiteCommand}" Background="Transparent" BorderThickness="0" IsEnabled="True"  BorderBrush="Transparent" >
                                    <!--<i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseDoubleClick">
                                            <i:InvokeCommandAction Command="{Binding DeleteSiteCommand}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>-->
                                    <Grid>

                                        <Image Source="/MasterLink;component/Resources/i_delete_UM.png" Height="16" Width="16"/>
                                    </Grid>

                                </Button>

1 个答案:

答案 0 :(得分:0)

您需要定义 canExecute 谓词,同时创建新的中继命令才能使其工作。您可以将其设置为true,以便在单击按钮时它将起作用。请尝试下面的代码。它应该工作。

deleteSiteCommand = new RelayCommand(this.DeleteSite, param => true);
selectionChangedCommand = new RelayCommand(this.FilterSite, param => true);

此外,您需要使用命令绑定才能使属性更改事件正常工作。

private ICommand deleteSiteCommand;
public ICommand DeleteSiteCommand
{
    get
    {
        return deleteSiteCommand;
    }
    set
    {
        SetProperty(ref deleteSiteCommand, value);
    }
}