【问题标题】:How to get Before Edit and After Edit of a Textbox?如何获得文本框的编辑前和编辑后?
【发布时间】:2019-04-12 11:10:07
【问题描述】:

我正在使用 PRISM MVVM 显示包含图像、文件名和图像大小的文件列表视图。

用户应该能够通过输入新名称来更改文件的名称。 离开文本框时,我的 ViewModel 中的文件名应该被重命名。为此,我当然需要了解之前和之后的文本。

我不想使用 Code behind,但我想我需要使用 GotFocus 来存储之前和 LostFocus 上的值以获得新值。对吧?

这是我的 XAML

  <Grid>   
    <ListView x:Name="MiscFilesListView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding MiscFiles}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <UniformGrid Columns="1" HorizontalAlignment="Stretch"/>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ListView.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Stretch">
            <Image Source="{Binding ImageData}" HorizontalAlignment="Center" VerticalAlignment="Top" Height="100" Width="100" />
            <TextBox Text="{Binding FileName}" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
            <TextBlock Text="{Binding Size}" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
          </StackPanel>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </Grid>

Listview 绑定到:

public ObservableCollection<MiscFile> MiscFiles
{
   get => _miscFiles;
   set => SetProperty(ref _miscFiles, value);
}

视图模型

  public class MiscFile : INotifyPropertyChanged
  {
    public BitmapImage ImageData { get; set; }
    public string FileName { get; set; }
    public string FullFileName { get; set; }
    public string Size { get; set; }

    public void OnPropertyChanged(string propertyname)
    {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }

    public event PropertyChangedEventHandler PropertyChanged;
  }

知道如何在 Viewmodel 中实现这一点吗? 我需要某种 EventTrigger 吗?

【问题讨论】:

  • 您是否考虑过使用 IEditableObject 作为模型类的基础,然后在保存时,您拥有数据的前/后?
  • 由于它似乎只是一个属性,因此您可以在新建 l 时设置一个原始值属性。

标签: c# wpf mvvm prism


【解决方案1】:

您可以在视图模型中为文件名创建一个私有字段。公共 FileName 属性应检查该值是否与私有字段中设置的值不同。同时通过调用OnPropertyChanged 通知 INotifyPropertyChanged。 这样做应该会更新文件名属性。

如果您想保留旧文件名,可以调用静态 Path 类的 Path.GetFileName(FullFileName) 方法。

private string _filename;
public BitmapImage ImageData
{
    get;set;
}

public string FileName
{
    get
    {
        return _filename;
    }
    set
    {
        if (_filename != value)
        {
            _filename = value;
            OnPropertyChanged(nameof(FileName));
        }
    }
}

【讨论】:

  • 感谢工作正常。没有考虑明显的问题。
【解决方案2】:

之前的值已经存储在您的MiscFile 对象中。只需为您的属性定义一个支持字段:

private string _filename;
public string FileName
{
    get { return _filename; }
    set
    {
        string oldValue = _filename;
        string newValue = value;

        //update...
        _filename = value;
    }
}

这应该可以工作,因为在 TextBox 失去焦点之前不应设置源属性,因为您尚未将绑定的 UpdateSourceTrigger 属性从其默认值 LostFocus 更改:

<TextBox Text="{Binding FileName, UpdateSourceTrigger=LostFocus}" ... />

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 2012-02-16
  • 1970-01-01
  • 2012-01-09
  • 2013-07-16
  • 1970-01-01
  • 2015-04-22
相关资源
最近更新 更多