私のWPFアプリケーションには、いくつかのデータバインドされたTextBoxがあります。これらUpdateSourceTrigger
のバインディングのはLostFocus
です。オブジェクトは、[ファイル]メニューを使用して保存されます。私が抱えている問題は、TextBoxに新しい値を入力し、[ファイル]メニューから[保存]を選択し、メニューにアクセスしてもTextBoxからフォーカスが削除されないため、新しい値(TextBoxに表示される値)を保持できないことです。 。どうすればこれを修正できますか?ページ内のすべてのコントロールを強制的にデータバインドする方法はありますか?
@palehorse:良い点です。残念ながら、必要なタイプの検証をサポートするために、UpdateSourceTriggerとしてLostFocusを使用する必要があります。
@dmo:私はそれについて考えていました。しかし、それは比較的単純な問題に対する本当にエレガントでない解決策のように思えます。また、フォーカスを受け取るために常に表示されるページ上のコントロールが必要です。ただし、私のアプリケーションはタブ付きであるため、そのようなコントロールはすぐには現れません。
@Nidonocu:メニューを使用してもTextBoxからフォーカスが移動しなかったという事実も、私を混乱させました。しかし、それは私が見ている行動です。次の簡単な例は、私の問題を示しています。
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="MyItemProvider" />
</Window.Resources>
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Save" Click="MenuItem_Click" />
</MenuItem>
</Menu>
<StackPanel DataContext="{Binding Source={StaticResource MyItemProvider}}">
<Label Content="Enter some text and then File > Save:" />
<TextBox Text="{Binding ValueA}" />
<TextBox Text="{Binding ValueB}" />
</StackPanel>
</DockPanel>
</Window>
using System;
using System.Text;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication2
{
public partial class Window1 : Window
{
public MyItem Item
{
get { return (FindResource("MyItemProvider") as ObjectDataProvider).ObjectInstance as MyItem; }
set { (FindResource("MyItemProvider") as ObjectDataProvider).ObjectInstance = value; }
}
public Window1()
{
InitializeComponent();
Item = new MyItem();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("At the time of saving, the values in the TextBoxes are:\n'{0}'\nand\n'{1}'", Item.ValueA, Item.ValueB));
}
}
public class MyItem
{
public string ValueA { get; set; }
public string ValueB { get; set; }
}
}