C# 具有LostFocus触发器的元素上的显式UpdateSource
我的场景是,在打开应用程序时,我从文件中读取了大量数据(使用C# 具有LostFocus触发器的元素上的显式UpdateSource,c#,wpf,binding,updatesourcetrigger,C#,Wpf,Binding,Updatesourcetrigger,我的场景是,在打开应用程序时,我从文件中读取了大量数据(使用XMLSerializer),并将其放入observateCollection。使用绑定,我向用户呈现数据。当他们更改字段中的数据时,将更新集合中的正确数据,但我不希望在LostFocus时将这些数据保存到文件中。我有一个“保存”按钮 我不想使用UpdateSOurceTrigger=PropertyChanged,我想保持LostFocus。问题是,当用户向文本框输入数据并按下保存按钮时,文本框不会失去焦点,这意味着数据不会传播到集合
XMLSerializer
),并将其放入observateCollection
。使用绑定,我向用户呈现数据。当他们更改字段中的数据时,将更新集合中的正确数据,但我不希望在LostFocus
时将这些数据保存到文件中。我有一个“保存”按钮
我不想使用UpdateSOurceTrigger=PropertyChanged
,我想保持LostFocus
。问题是,当用户向文本框
输入数据并按下保存按钮时,文本框
不会失去焦点,这意味着数据不会传播到集合,也不会保存。下面是我的解决方案,但我的问题是,这是一种正确的方法,还是有其他更好的方法
在保存集合之前,我添加到“保存”按钮的代码:
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>
那么这里到底发生了什么:
如您所见,TextBox
就在那里,它的UpdateSourceTrigger
被设置为LostFocus
当我修改那里的值并单击Save按钮时,我可以看到发生了什么,Save方法中有一个断点,值会被更新,因为当你单击按钮时,文本框
会失去焦点
不管怎样,我想给你写这封信,因为可能还有更多的东西
查看IEditableObject
界面:
提供提交或回滚对以下对象所做更改的功能:
用作数据源
就这样
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();
}
}