Sim*_* D. 10 c# data-binding wpf textbox focus
当我无法使用UpdateTrigger = PropertyChanged进行绑定时,我观察到绑定到textproperties的文本框的一些意外或至少不完全匹配的my-needs行为.可能它不是文本框的问题,但也会与其他编辑器一起出现.
在我的示例(附带源代码)中,我有一个绑定到某个集合的WPF TabControl.在每个选项卡上,您可以从集合中编辑项目,以各种方式触发保存操作,这应该将编辑保存到某个模型.绑定到每个项目属性的文本框(有意)保持默认更新触发器'OnFocusLost'.这是因为在设置新值时会发生一些昂贵的验证.
现在我发现至少有两种方法可以以这种方式触发我的保存操作,即最后一个聚焦文本框不会更新绑定值.1)通过鼠标单击其标题更改选项卡项,然后单击某个保存按钮.(更改回上一个选项卡显示新值甚至丢失)2)通过KeyGesture触发save-command.
我设置了一个演示行为的示例应用程序.单击"全部保存"将显示所有项目值,另一个保存按钮仅显示当前项目.
问:在绑定对象被调用之前,确保所有文本框的所有绑定源都会更新的最佳方法是什么?最好应该采用一种方式捕捉所有可能性,我不喜欢以不同的方式捕捉每个事件,因为我担心会忘记一些事件.例如,观察选项卡控件的选择更改事件将解决问题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>
Run Code Online (Sandbox Code Playgroud)
和相应的课程
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
也许回答自己的问题不太好,但我认为这个答案比其他答案更适合这个问题,因此值得写。当然这也是因为我把问题描述得不够清楚。
最后,作为一个快速的概念证明,我像这样解决了这个问题:当我切换选项卡时,LostFocus-Event 永远不会在 TextBox 上触发。因此,绑定不会更新,输入的值也会丢失,因为切换回来会使绑定从其源刷新。但是触发的是 PreviewLostFocus-Event,因此我迷上了这个小函数,它手动触发对绑定源的更新:
private void BeforeFocusLost(object sender, KeyboardFocusChangedEventArgs e)
{
if (sender is TextBox) {
var tb = (TextBox)sender;
var bnd = BindingOperations.GetBindingExpression(tb, TextBox.TextProperty);
if (bnd != null) {
Console.WriteLine(String.Format("Preview Lost Focus: TextBox value {0} / Data value {1} NewFocus will be {2}", tb.Text, bnd.DataItem, e.NewFocus));
bnd.UpdateSource();
}
Console.WriteLine(String.Format("Preview Lost Focus Update forced: TextBox value {0} / Data value {1} NewFocus will be {2}", tb.Text, bnd.DataItem, e.NewFocus));
}
}
Run Code Online (Sandbox Code Playgroud)
根据带有 PreviewLostFocus、LostFocus(均来自 TextBox)和 SelectionChanged(来自 TabControl)的事件链的输出将如下所示:
预览失去焦点:文本框值 Smith123456 / 数据值 John Smith123 NewFocus 将是 System.Windows.Controls.TabItem 标题:Peter Miller 内容:Peter Miller 预览失去焦点 强制更新:文本框值 Smith123456 / 数据值 John Smith123456 NewFocus 将是 System.Windows .Controls.TabItem 标题:Peter Miller 内容:Peter Miller 选择从 System.Object[] 更改为 System.Object[] 预览失去焦点:TextBox 值 Miller / 数据值 Peter Miller 新焦点将是 System.Windows.Controls.TextBox:Peter预览失去焦点强制更新:TextBox 值 Miller / 数据值 Peter Miller NewFocus 将是 System.Windows.Controls.TextBox:Peter Lost Focus 具有值 Miller
我们看到LostFocus只发生在最后,而不是在改变TabItem之前。我仍然认为这很奇怪,可能是 WPF 或标准控件模板中的错误。谢谢大家的建议,抱歉,我无法真正将它们签名为答案,因为它们没有解决制表符更改时条目丢失的问题。
| 归档时间: |
|
| 查看次数: |
3737 次 |
| 最近记录: |