【问题标题】:Perform actions while in inserting/editing in MVVM在 MVVM 中插入/编辑时执行操作
【发布时间】:2016-04-18 21:59:36
【问题描述】:

我正在使用 C#、WPF 和 MVVM 开发一个简单的 CRUD。我使用 DataGrid 进行导航,使用 Databounded Textboxes,使用“New”按钮进行插入,用户可以简单地更改文本框的值来更改数据,最后单击“Save”按钮。

现在,我可以在单击“新建”按钮后轻松禁用 DataGrid,并在单击“保存”按钮后重新启用。

但是,版本呢?我想在编辑时禁用 DataGrid,但我不知道如何在 MVVM 中做到这一点。

  1. 我在视图中执行此操作,我从文本框中查看了一些“PropertyChanged”?
  2. 我在 ViewModel 中执行此操作,然后从 Entity 属性中查看一些“PropertyChanged”(我已经为我的实体实现了 INotifyPropertyChanged)?
  3. 另一种选择?

【问题讨论】:

    标签: c# wpf mvvm datagrid


    【解决方案1】:

    您可以默认将文本框设为只读,并且仅在用户进入editcreate new 状态时启用它们。

    顺便说一句,当DataGrid 已经支持时,您不应该实现 CRUD 接口。

    编辑:帮助您可视化的代码

    XAML

    <__CONTAINER__.Resources>
        <Style x:Key="CrudTextBoxStyle"
               BasedOn="{StaticResource {x:Type TextBox}}"
               TargetType="TextBox">
            <Setter Property="IsEnabled" Value="False" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding EditMode}" Value="CreateNew">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
                <DataTrigger Binding="{Binding EditMode}" Value="Edit">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </__CONTAINER__.Resources>
    
    
    <DataGrid IsReadOnly="True"
              ItemsSource="{Binding Records}"
              SelectedItem="{Binding CurrentRecord}"
              SelectionMode="Single"
              SelectionUnit="FullRow" />
    
    <Button Content="Create New" />
    <Button Content="Edit" />
    <Button Content="Save" />
    <Button Content="Cancel" />
    
    <TextBox Style="{StaticResource CrudTextBoxStyle}" Text="{Binding CurrentRecord.TextProperty1}" />
    <TextBox Style="{StaticResource CrudTextBoxStyle}" Text="{Binding CurrentRecord.TextProperty2}" />
    

    背后的代码

    class CrudViewModel
    {
        // you should use the full implementation with INPC
        public Record CurrentRecord { get; set; }
        public IList<Record> Records { get; set; }
        public EditMode EditMode { get; set; }
    
        private void CreateNewImpl()
        {
            CurrentRecord = new Record();
            EditMode = EditMode.CreateNew;
        }
        private void EditImpl()
        {
            EditMode = EditMode.Edit;
        }
        private void SaveImpl()
        {
            if (EditMode == EditMode.CreateNew)
            {
                Records.Add(CurrentRecord);
            }
    
            EditMode = EditMode.View;
        }
    }
    
    enum EditMode
    {
        View, CreateNew, Edit
    }
    

    【讨论】:

    • 当你说“编辑”或“创建新”状态时,是来自 DbContext 吗?
    • 没有。它应该是一个枚举,您可以将其作为视图模型中的属性公开。
    • 我理解你,但我的问题是关于那个“编辑”按钮。现在我没有使用编辑按钮,用户可以简单地修改文本框并保存,所以我正在考虑检测文本框(或模型,idk)中的更改以“将状态更改为编辑模式” .我会在你的只读文本框解决方案中考虑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2021-07-06
    • 2019-02-19
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多