我觀察到一些意想不到的或至少不,完美匹配 - 我 - 的需求勢必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