我观察到一些意想不到的或至少不,完美匹配 - 我 - 的需求势必textproperties当我不能使用使用文本框的行为的任何行动UpdateTrigger =的PropertyChanged之前更新的TextBox的BindingSource为了我的约束。可能它不是文本框的问题,但也会出现在其他编辑器中。如何实现重点复位
在我的例子(附源代码),我有一个WPF的TabControl绑定到一些集合。 在每个选项卡上,您都可以通过各种方式编辑集合中的项目,从而触发保存操作,并将编辑保存到某个模型中。 绑定到每个项目属性的文本框(有意)保持为默认的更新触发器'OnFocusLost'。这是因为在设置新值时会发生一些昂贵的验证。
现在我发现至少有两种方法可以触发我的拯救行动以这样的方式,即最后的焦点文本框不更新绑定值。 1)通过鼠标点击其标题,然后点击一些保存按钮来更改标签项。 (更改回上一个标签显示新值甚至丢失) 2)通过KeyGesture触发保存命令。
我设置了一个演示行为的示例应用程序。点击“全部保存”将显示所有项目值,另一个保存按钮仅显示当前项目。
问:什么是确保被comitted绑定的对象之前,我所有的文本框的所有bindingsources将被更新的最佳方式? 最好应该有一种方法捕捉所有可能性,我不喜欢以不同方式捕捉每个事件,因为我担心已经忘记了一些事件。 观察选项卡控件的选择更改事件,例如将解决问题1),但不解决问题2)。
我们的例子:
XAML第一:
<Window x:Class="TestOMat.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TestOMat="clr-namespace:TestOMat"
Title="TestOMat" x:Name="wnd">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="dtPerson" DataType="{x:Type TestOMat:Person}">
<StackPanel Orientation="Vertical">
<StackPanel.CommandBindings>
<CommandBinding Command="Close" Executed="CmdSaveExecuted"/>
</StackPanel.CommandBindings>
<TextBox Text="{Binding FirstName}"/>
<TextBox Text="{Binding LastName}"/>
<Button Command="ApplicationCommands.Stop" CommandParameter="{Binding}">Save</Button>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.CommandBindings>
<CommandBinding Command="ApplicationCommands.Stop" Executed="CmdSaveAllExecuted"/>
</Grid.CommandBindings>
<TabControl ItemsSource="{Binding ElementName=wnd, Path=Persons}" ContentTemplate="{StaticResource dtPerson}" SelectionChanged="TabControl_SelectionChanged"/>
<Button Grid.Row="1" Command="ApplicationCommands.Stop">Save All</Button>
</Grid></Window>
而且相应的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TestOMat
{
/// <summary>
/// Interaction logic for TestOMat.xaml
/// </summary>
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
}
private List<Person> persons = new List<Person>
{
new Person {FirstName = "John", LastName = "Smith"},
new Person {FirstName = "Peter", LastName = "Miller"}
};
public List<Person> Persons
{
get { return persons; }
set { persons = value; }
}
private void CmdSaveExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
Person p = e.Parameter as Person;
if (p != null)
{
MessageBox.Show(string.Format("FirstName={0}, LastName={1}", p.FirstName, p.LastName));
e.Handled = true;
}
}
private void CmdSaveAllExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
MessageBox.Show(String.Join(Environment.NewLine, Persons.Select(p=>string.Format("FirstName={0}, LastName={1}", p.FirstName, p.LastName)).ToArray()));
e.Handled = true;
}
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Console.WriteLine(String.Format("Selection changed from {0} to {1}", e.RemovedItems, e.AddedItems));
// Doing anything here only avoids loss on selected-tab-change
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
为了方便起见,我使用了Stop-Command,s.t.对于大多数用户按[Esc]应触发该操作。 – 2009-07-15 20:00:31