【发布时间】: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 时设置一个原始值属性。