更改DataGrid时,使用MessageBox添加一个检查

时间:2017-03-01 09:51:50

标签: c# wpf mvvm

我已经发布了一个桌面应用程序(所以我很感激能够将更改和回归测试保持在最低限度的答案)并且我需要在网格更改时添加一致性检查CanBeDeleted

<DataGrid AutoGenerateColumns="False" 
    ItemsSource="{Binding CurrentPosIn.PosInLocationsList}" 
    CanUserAddRows="{Binding UpdateEnabled}" CanUserDeleteRows="{Binding UpdateEnabled}" >

我正在使用UpdateEnabled用于不同的内容(配置文件权限),我也不想让DataGrid只读取:我更喜欢(除非它太复杂了)查看阻止更改的阻止警报(MessageBox)。

我到目前为止所做的是

  1. 针对MVVM,因为我已将警报放入模型中(但我可以接受,如果它使更改变得简单快捷)
  2. 无法正常工作,因为我的更改的第二部分(见下文)会产生无效的操作异常
  3. ViewModel包含以下列表

        [Association(ThisKey="Partita", OtherKey="Partita", CanBeNull=true, IsBackReference=true)]
        public ObservableCollection<Model.PosInLocation> posin_locations_list = new  ObservableCollection<Model.PosInLocation>(); 
        public ObservableCollection<PosInLocation> PosInLocationsList {
            get { return posin_locations_list; }
            set { 
                posin_locations_list = value;
                OnPropertyChanged( () => PosInLocationsList );
            }
        } 
    

    我在这里添加一致性检查

        string _storage;
        [Column(Name = "storage"), PrimaryKey]
        public string Storage {
            get { return _storage; }
            set { 
                if (this.loadedEF) {
                    string validate_msg;
                    if (!PurchasePosIn.CanBeDeleted(out validate_msg)) {
                        // against MVVM
                        MessageBox.Show(validate_msg, "Alert", MessageBoxButton.OK); 
                        OnPropertyChanged( () => Storage  );
                        return;
                    }
                    Persistence.MyContext.deletePosInLocation(this);
                }
                _storage = value;
                OnPropertyChanged( () => Storage  );
                if (this.loadedEF) {
                    Persistence.MyContext.insertPosInLocation(this);
                }               
            }
        }
    

    和这里(第二部分)

        internal void posin_locations_list_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
        {                   
            string validate_msg;
            if (!CanBeDeleted(out validate_msg)) {
                // indirectly produces an invalid operation exception 
                MessageBox.Show(validate_msg, "Alert", MessageBoxButton.OK);
                return;
            }
            if (args.OldItems != null)
                foreach(var oldItem in args.OldItems) {
                    if ( ((PosInLocation)oldItem).Partita != null)        
                        Persistence.MyContext.deletePosInLocation((PosInLocation)oldItem);              
                }       
            if (args.NewItems != null)
                foreach(var newItem in args.NewItems)
                {
                    PosInLocation newPosInLocation = (PosInLocation)newItem;
                    if ( newPosInLocation.Partita == null) {
                        newPosInLocation.Partita = this.Partita;
                        newPosInLocation.PurchasePosIn = this;
                        newPosInLocation.loadedEF = true;
                    }
                 }
        }
    

3 个答案:

答案 0 :(得分:0)

也许它很丑(真的,它不是那么难看:imho它是一个很好的MVVM approach,也适用于现代mahapps.metro Dialogs),但现在我正在设置

if (!CanBeDeleted(out validate_msg)) {
    PurchaseItem.MessageBoxText = validate_msg;

隐形TextBox

    <TextBox Visibility="Hidden" Name="tb_message"
        Text="{Binding MessageBoxText}"
        TextChanged="TextBox_TextChanged"

我正在发送警报

    void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string alert = tb_message.Text;
        if (alert != null && tb_message.Text.Length>0) {
            Dispatcher.BeginInvoke(
                (Action)(() => { 
                            MessageBox.Show(alert, "Alert", MessageBoxButton.OK);
                            tb_message.Text = "";
                         }));
        }
    }

回滚添加/删除的项目

我看到与其他问题Prevent adding the new Item on ObservableCollection.CollectionChanged event的联系,在我的情况下,我会说防止删除更为重要。我不知道是否有比这个更新的答案(Can I rollback collection changes on collection changed event?出现wrong)有关此主题的更新。

虽然可以轻松提升PropertyChanged以回滚项目更新,但对于集合更改,我被迫传递并引用CollectionChanged事件中的视图调度程序

PurchaseItem.dispatcher.BeginInvoke((Action)(() => RollBack(args)));

回滚添加/删除的项目

    bool rollingBack = false;
    private void RollBack(NotifyCollectionChangedEventArgs args) {
            rollingBack = true;
            if (args.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (var element in args.OldItems) {
                    PosInLocationsList.Add((PosInLocation)element);
                }
            }
            if (args.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (var element in args.NewItems) {
                    PosInLocationsList.Remove((PosInLocation)element);
                }
            }
            rollingBack = false;
    }

答案 1 :(得分:0)

如果只有ObservableCollection实现了“previewCollectionChanged”,那么事情会容易得多。

根据您的需要,我建议创建一个ObservableCollection的子类并重载受保护的方法RemoveItem 根据您对应用程序的操作,您可能希望覆盖除RemoveItem之外的其他方法(如ClearItems)。

子类化ObservableCollection时,可以覆盖5个受保护的方法:ClearItems,RemoveItem,InsertItem,SetItem和MoveItem。
最后,这些方法被所有公共方法调用,因此覆盖它们可以让您完全控制。

这是一个可以运行的小应用程序,用于演示:

ObservableCollection SubClass

public class ObservableCollectionWithDeletionControl<T> : ObservableCollection<T>
{
    public delegate void DeletionDeniedEventHandler(object sender, int indexOfDeniedDeletion);
    public event DeletionDeniedEventHandler DeletionDenied;

    public bool CanDelete { get; set; }

    protected virtual void OnDeletionDenied(int indexOfDeniedDeletion)
    {
        if (DeletionDenied != null) { DeletionDenied(this, indexOfDeniedDeletion); }
    }

    protected override void RemoveItem(int index)
    {
        if (CanDelete)
        {
            base.RemoveItem(index);
        }
        else
        {
            OnDeletionDenied(index);
        }
    }
}

我使用DeletionDenied事件,以便此类不负责显示错误窗口,并使其更具可重用性。

视图模型

public class MainWindowViewModel
{
    public MainWindow window { get; set; }

    public ObservableCollectionWithDeletionControl<Person> People { get; set; } = new ObservableCollectionWithDeletionControl<Person>();

    public MainWindowViewModel()
    {
        People.DeletionDenied += People_DeletionDenied;
    }

    private void People_DeletionDenied(object sender, int indexOfDeniedDeletion)
    {
        Person personSavedFromDeletion = People[indexOfDeniedDeletion];
        window.displayDeniedDeletion(personSavedFromDeletion.Name);
    }
}

MainWindow的ViewModel 它知道它的窗口只是为了显示错误信息。
(我确信有一个比这更好的解决方案,但我还没有找到一个显示弹出窗口的简短方法MVVM。)
当DeletionDenied事件触发时,将调用错误窗口。

模型

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get { return _name; }
        set
        {
            if(_name == value) { return; }
            _name = value;
            if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); }
        }
    }

    private string _name = "";
}


XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <CheckBox DockPanel.Dock="Top" Content="Can delete" IsChecked="{Binding People.CanDelete}" Margin="5" HorizontalAlignment="Left"/>
        <DataGrid ItemsSource="{Binding People}" Margin="5,0"/>
    </DockPanel>
</Window>


XAML.CS

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void displayDeniedDeletion(string name)
    {
        TextBox errorMessage = new TextBox();
        errorMessage.Text = string.Format("Cannot delete {0} : access denied !", name);

        Window popupErrorMessage = new Window();
        popupErrorMessage.Content = errorMessage;

        popupErrorMessage.ShowDialog();
    }
}

APP MAIN

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        MainWindow window = new MainWindow();
        MainWindowViewModel viewModel = new MainWindowViewModel();

        viewModel.window = window;
        window.DataContext = viewModel;
        window.Show();

        App.Current.MainWindow = window;
    }
}

我在启动时设置了ViewModel的窗口,但你应该在创建ViewModel的任何地方都这样做(

答案 2 :(得分:0)

  

到目前为止我所做的是

     

反对MVVM,因为我已将警报放在模型中

来自@Tesseract,子类化ObservableCollection和订阅RemoveItem的解决方案已经面向MVVM。

仍然缺少一种从ViewModel发送警报的正确,现代的方法。这是Mahapps approach有用的地方。

  • 使用窗口中的附加属性注册视图模型 使用对话框子系统。

在你的XAML中

Dialog:DialogParticipation.Register="{Binding}"

附加属性DialogPartecipation将通过字典跟踪视图

public static class DialogParticipation
{
    private static readonly IDictionary<object, DependencyObject> ContextRegistrationIndex = new Dictionary<object, DependencyObject
  • 您可以直接实例化DialogCoordinator或注入 接口IDialogCoordinator进入他们的视图模型

DialogCoordinator会将ViewModel与视图匹配

public class DialogCoordinator : IDialogCoordinator
{
    public static readonly IDialogCoordinator Instance = new DialogCoordinator();

通过上述附加属性(context是视图模型)

var association = DialogParticipation.GetAssociation(context);

并显示对话框,在检索到的视图上调用适当的方法:如果打开多个窗口,则对话框将显示在正确的窗口上。