【问题标题】:CancelEdit on Datagrid MVVM?在 Datagrid MVVM 上取消编辑?
【发布时间】:2019-09-30 14:29:27
【问题描述】:

当布尔返回为假时,我正在尝试取消编辑。一旦我遍历选定的单元格并进行了一些计算,布尔值就会设置为 false。这是在 CellEditEnding 函数中,但我正在使用 EventTrigger 交互性,所以它都是 MVVM 并且当这个 bool 为 false 时,我没有 datagrid 对象来实际执行 CancelEdit 调用。

有什么方法可以取消使用 MVVM 的编辑?当我从单元格中失去焦点时,它仍然是白色的......

这就是我目前的解决方法......虽然不喜欢它......

在代码中我这样做...我只是不喜欢在视图模型中这样做...有点破坏 MVVM

    public void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        bool isSuccess = true;

        if (e.EditAction == DataGridEditAction.Commit)
        {
            if (e.EditingElement is TextBox cellTextEdit && cellTextEdit.Text != string.Empty)
            {
                for (int iSelected = 0; iSelected < SelectedGridCellCollection.Count; ++iSelected)
                {
                     if (!isSuccess)
                     {
                         e.Cancel = true;

                          // Only "non" MVVM here, but if there is another way to do this then I'd
                         // be surprised...
                         DataGrid dg = sender as DataGrid;
                         dg.CancelEdit();
                      }
                 }
            }
       }
   }

在 XAML 中

                                <DataGrid Name="dataGrid" Width="550" ItemsSource="{Binding ObjSource}" 
                                          CanUserSortColumns="False" CanUserAddRows="false" 
                                          HorizontalAlignment="Stretch" 
                                          VerticalScrollBarVisibility="Auto"
                                          utils:DataGridSelectedCellsBehavior.SelectedCells="{Binding SelectedGridCellCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                          AutoGenerateColumns="False" 
                                          Background="{StaticResource ResourceKey=Polour}"
                                          SelectionUnit="Cell" 
                                          RowHeaderWidth="0" 
                                          RowStyle="{StaticResource DataGridRowStyle}" 
                                          AlternationCount="2" 
                                          FontFamily="{StaticResource ResourceKey=Light}" 
                                          CanUserResizeColumns="False" CanUserReorderColumns="False">
                                    <DataGrid.Resources>
                                        <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
                                            <Setter Property="Background" Value="{StaticResource ResourceKey=Polour}"/>
                                            <Setter Property="Foreground" Value="White"/>
                                            <Setter Property="BorderBrush" Value="{StaticResource ResourceKey=Solour}"/>
                                            <Setter Property="BorderThickness" Value="0,0,0,1"/>
                                            <Setter Property="HorizontalAlignment" Value="Center"/>
                                            <Setter Property="VerticalAlignment" Value="Center"/>
                                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                            <Setter Property="VerticalContentAlignment" Value="Center"/>
                                            <Setter Property="Margin" Value="5 0 5 0"/>
                                            <Setter Property="Height" Value="30"/>
                                            <Setter Property="Width" Value="Auto"/>
                                        </Style>
                                    </DataGrid.Resources>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="ll" Binding="{Binding Id}" IsReadOnly="True"></DataGridTextColumn>
                                    </DataGrid.Columns>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="CellEditEnding">
                                            <i:CallMethodAction TargetObject="{Binding}" MethodName="CellEditEnding"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </DataGrid>

【问题讨论】:

  • 为什么投反对票:\
  • 如果使用mvvm 模式,你会有绑定等。我没有看到任何?
  • stackoverflow.com/questions/1048517/… 但您仍然会因为必须引用 DataGrid 类而破坏 MVVM。
  • 我同意,这种否决投票在实现方式上并不是一个好的功能。人们使用它太频繁了。最糟糕的是,他们没有提供有助于改进的解释。那不是批评家。如果有关于如何改进问题的建议,那就更好了。所以你必须忽略它。但我能感觉到这是如何造成挫败感的。
  • 尝试总是发布有助于重现问题的编译代码和解释(或代码 cmets)。这应该使您远离大多数反对票。如果您需要帮助,请尝试帮助给您一个好的答案,而不会浪费一次时间。这有时会很烦人。

标签: c# wpf xaml mvvm


【解决方案1】:

https://www.codeproject.com/Questions/487418/WPFplusDataGridplusProblempluswhenplushiddenplusan

如果有人正在考虑以这种方式编辑 Datagrids,解决方案 2 解决了我的问题 - 尽管 BionicCode 的建议对于用户体验来说要好得多

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多