【问题标题】:WPF App using MVVM - Prevent binding from editing DataGrid record when bound TextBox is edited使用 MVVM 的 WPF 应用程序 - 编辑绑定的 TextBox 时防止绑定编辑 DataGrid 记录
【发布时间】:2016-04-18 14:38:02
【问题描述】:

好的,所以我有一个使用 MVVM 模式的 WPF 应用程序。

我有一个 DataGrid,ItemsSource 绑定到 ObservableCollection,SelectedItem 绑定到我的模型的一个属性,称为 CurrentCategory

 <DataGrid x:Name="LstCategories" Grid.Column="0" Grid.Row="1"  AutoGenerateColumns="false"  IsReadOnly="True"
              ItemsSource="{Binding Path=ReceivedCategories, Mode=TwoWay}" 
              HorizontalScrollBarVisibility="Disabled" GridLinesVisibility="None"
              CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="True" Background="White"
              SelectedIndex="{Binding Path=SelectedIndex}" 
                      SelectedItem="{Binding CurrentCategory, UpdateSourceTrigger=PropertyChanged}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=Description}" IsReadOnly="True" Header="Description" Width="300" />
                </DataGrid.Columns>
            </DataGrid>

TextBox 文本绑定到 CurrentCategory 的 Description 属性。

<TextBox x:Name="TbDescription" Grid.Row="0" Grid.Column="1" Width="250" Height="Auto" 

                     VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"
                     Text="{Binding Path=CurrentCategory.Description, Mode=TwoWay}" />

最后是一个按钮,用于将描述更新到数据库。

  <Button   Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" 
                      Height="20" Width="120" Content="Update Description" 
                      Command="{Binding UpdateCategoryCommand}"
                      CommandParameter="{Binding Path=CurrentCategory.CategoryId}" />

这一切都按预期工作。

但是,我不希望在更改 TextBox 中的文本时自动编辑 DataGrid 记录。

因此,将绑定设置为 One-Way 是合乎逻辑的。但是当我点击更新按钮时,Description 属性仍然是旧值。

我尝试了各种数据绑定模式,但无法获得我想要的确切行为。

似乎是第 22 条问题。

关于获得我所追求的行为的任何想法?

编辑 1 - 工作解决方案

正如 Amine 所说,我添加了一个额外的属性并将 TextBox 绑定到它。我还必须检查 CurrentCategory 中的值是否不为空。可能是因为绑定到 SelectedItem 和 ItemsSource 并且当重新填充 DataGrid 时,它绑定到任何内容,使其为空。反正我的理论。

最后更改了检查是否应启用更新按钮的方法,以及更新到数据库的方法以使用 EditedCategory 属性。

工作代码:

    private CategoryModel _currentCategory;
    public CategoryModel CurrentCategory
    {
        get { return _currentCategory; }
        set
        {
            if (value != null)
            {
                _currentCategory = value;

                EditedCategory = new CategoryModel
                {
                    CategoryId = value.CategoryId,
                    Description = value.Description
                };

                OnPropertyChanged("CurrentCategory");
            }
        }        
    }

    private CategoryModel _editedCategory;

    public CategoryModel EditedCategory
    {
        get { return _editedCategory; }
        set
        {
            _editedCategory = value;
            OnPropertyChanged("EditedCategory");
        }
    }


    /// <summary>
    /// Checks whether the Update button should be enabled or disabled
    /// </summary>
    /// <returns>True or False</returns>
    public bool CanUpdateCategory()
    {
        return !String.IsNullOrWhiteSpace(EditedCategory.Description);
    }

    /// <summary>
    /// Method to update the selected category
    /// </summary>
    /// <param name="id"></param>
    public void UpdateCategory(int id)
    {
        if (CurrentCategory.UpdateCategory(id, EditedCategory.Description))
            GetCategories();
    }

【问题讨论】:

  • 我认为您需要使用UpdateSourceTrigger=Explicit 并在单击刷新按钮或单击更新按钮时通知您的属性。
  • 我试过了,仍然无法获得我想要的确切行为,或者我没有正确使用它。

标签: wpf mvvm data-binding


【解决方案1】:

您需要另一个属性,例如:“EditedCategory”。 当您在 DataGrid 中选择一个项目时,您必须为 EditedCategory 创建一个新实例。

private object currentCategory;
public object CurrentCategory
{
    get { return currentCategory; }
    set
    {
        currentCategory = value;
        EditedCategory = new object{ id = value.id....}
        this.OnPropertyChanged("CurrentCategory"); 

    }
}

private object editedCategory;
public object EditedCategory
{
    get { return editedCategory; }
    set
    {
        editedCategory = value;
        this.OnPropertyChanged("EditedCategory");

    }
}

然后你将 TexBox 绑定到 EditedCategory。

【讨论】:

  • 这很好用。另一件事是在 CurrentCategory 中,我必须在设置 currentCategory = value 之前添加检查以查看 value 是否为 null。将添加上面的工作代码。谢谢阿明。
猜你喜欢
  • 2016-04-11
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2015-11-07
  • 2019-12-04
  • 2013-01-30
  • 1970-01-01
相关资源
最近更新 更多