c# - 具有 LostFocus 触发器的元素上的显式 UpdateSource

标签 c# wpf binding updatesourcetrigger

我的情况是,在打开应用程序时,我从文件中读取了大量数据(使用 XMLSerializer )并将其放入 ObservableCollection<MyClass> 中.使用绑定(bind),我向用户呈现数据。当他们更改字段中的数据时,集合中的适当数据会更新,但我不希望将这些数据保存到文件中 LostFocus .我有一个“保存”按钮。

我不想使用 UpdateSOurceTrigger = PropertyChanged , 我想保留 LostFocus .问题是,当用户向 TextBox 输入数据时并按下保存按钮,TextBox不会失去焦点,这意味着数据不会传播到集合并且不会被保存。以下是我的有效解决方案,但我的问题是,这是一种正确的做法,还是有其他更好的方法?

我在保存集合之前添加到“保存”按钮的代码:

IInputElement focusedElement = Keyboard.FocusedElement;

if (focusedElement is TextBox)
{
    BindingExpression myBinding = BindingOperations.GetBindingExpression((TextBox)focusedElement, TextBox.TextProperty);
    myBinding.UpdateSource();
}

最佳答案

这是您的场景的一个小示例:

<Window x:Class="MementoAndLostFocus.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MementoAndLostFocus"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="10*"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBox Text="{Binding ExampleInstance.ExampleName, UpdateSourceTrigger=LostFocus}"
             BorderThickness="1"
             BorderBrush="Black"
             VerticalAlignment="Top"
             Margin="10"/>
    <Button Grid.Row="1"
            HorizontalAlignment="Left" 
            VerticalAlignment="Center" 
            Content="Save"
            Command="{Binding SaveCommand}"/>
</Grid>

public class MainViewModel
{
    public ExampleClass ExampleInstance { get; set; }
    public ICommand SaveCommand { get; set; }

    public MainViewModel()
    {
        ExampleInstance = new ExampleClass() { ExampleName = "Example Name" };
        SaveCommand = new SaveCommand(this);
    }

    internal void Save()
    {
        //TO DO  - Save item to database
        Console.WriteLine(ExampleInstance.ExampleName);
    }
}

public class ExampleClass : INotifyPropertyChanged
{
    private string _exampleName;

    public string ExampleName
    {
        get { return _exampleName; }
        set
        {
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(ExampleName)));
            _exampleName = value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

public class SaveCommand : ICommand
{
    private MainViewModel _vm;

    public event EventHandler CanExecuteChanged;

    public SaveCommand(MainViewModel vm)
    {
        _vm = vm;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _vm.Save();
    }
}

那么这里到底发生了什么:

如您所见,TextBox 就在那里,它的 UpdateSourceTrigger 设置为 LostFocus

当我修改那里的值并单击“保存”按钮时,我能够看到发生了什么,在“保存”方法中有一个断点并且值被更新,因为当您单击按钮时, TextBox 失去焦点。

enter image description here

enter image description here

无论如何,我想写这篇文章给你,因为可能还有更多内容。

看看 IEditableObject 接口(interface):

Provides functionality to commit or rollback changes to an object that is used as a data source.

就是这样。

关于c# - 具有 LostFocus 触发器的元素上的显式 UpdateSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541112/

相关文章:

c# - LINQ to SQL : Multiple joins ON multiple Columns. 这可能吗?

c# - 事件不会在自定义用户控件中触发

c# - 多次未分配局部变量?

c# - 在 WPF 中使用数据绑定(bind)刷新 UI

WPF dataGgridCell 编辑模式

swiftui - 如何修复无法将类型 'Binding<Device>' 的值转换为预期的参数类型 'Device'

c# - 将匿名委托(delegate)与 .NET ThreadPool.QueueUserWorkItem 结合使用

wpf - 为什么在实现 INotifyPropertyChanged 时 WPF 似乎绕过了 TypeDescriptionProviderAttribute?

c# - DataTemplate 内的 DataTemplate - ListBox 内的 ListBox

c# - 尽管在XAML中为UI元素设置了 “”,但GetValue(NameProperty)返回 “x:Name”