2017-06-29 92 views
0

我在放入TabControl的TextBox控件中出現了一個奇怪的行爲。
當我更改TextBox控件值並且不更改焦點時,在更改選項卡上我鬆開了此TextBox中的更改。
當我改變焦點並更改選項卡時 - 所有內容都已保存。WPF,TabControl和Binding

請參見如GIF:

Example gif

這裏是我的代碼:
的XAML:

<Window x:Class="TabPanelTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TabControl Name="tabs" ItemsSource="{Binding}"> 
      <TabControl.ContentTemplate> 
       <DataTemplate> 
        <StackPanel Background="{Binding ContainterColor}"> 
         <TextBox Text="{Binding Val1, Mode=TwoWay}" Margin="20 10"/> 
         <TextBox Text="{Binding Val2, Mode=TwoWay}" Margin="20 10"/> 
         <TextBox Text="{Binding Val3, Mode=TwoWay}" Margin="20 10"/> 
        </StackPanel> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
     </TabControl> 
    </Grid> 
</Window> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace TabPanelTest 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      MyClass myObj1 = new MyClass("Tab1"); 
      MyClass myObj2 = new MyClass("Tab2"); 

      myObj1.ContainterColor = new SolidColorBrush(Colors.LightGreen); 
      myObj2.ContainterColor = new SolidColorBrush(Colors.LightBlue); 

      myObj1.Val1 = "Tab1 Val1"; 
      myObj1.Val2 = "Tab1 Val2"; 
      myObj1.Val3 = "Tab1 Val3"; 

      myObj2.Val1 = "Tab2 Val1"; 
      myObj2.Val2 = "Tab2 Val2"; 
      myObj2.Val3 = "Tab2 Val3"; 

      InitializeComponent(); 
      tabs.DataContext = new MyClass[] { myObj1, myObj2 }; 
      tabs.SelectedIndex = 0; 


     } 
    } 

    public class MyClass: INotifyPropertyChanged 
    { 
     public string Name { get; set; } 

     Brush _containerColor; 
     string val1; 
     string val2; 
     string val3; 

     public Brush ContainterColor 
     { 
      get 
      { 
       return _containerColor; 
      } 
      set 
      { 
       if(value!=_containerColor) 
       { 
        _containerColor = value; 
        OnPropertyChanged("ContainterColor"); 
       } 
      } 
     } 

     public string Val1 
     { 
      get 
      { 
       return val1; 
      } 
      set 
      { 
       if (value != val1) 
       { 
        val1 = value; 
        OnPropertyChanged("Val1"); 
       } 
      } 
     } 
     public string Val2 
     { 
      get 
      { 
       return val2; 
      } 
      set 
      { 
       if (value != val2) 
       { 
        val2 = value; 
        OnPropertyChanged("Val2"); 
       } 
      } 
     } 

     public string Val3 
     { 
      get 
      { 
       return val3; 
      } 
      set 
      { 
       if (value != val3) 
       { 
        val3 = value; 
        OnPropertyChanged("Val3"); 
       } 
      } 
     } 

     public override string ToString() 
     { 
      return Name; 
     } 

     public MyClass(string name) 
     { 
      Name = name; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

如果焦點沒有改變,我需要做什麼來保存更改?

非常感謝!

更新:

非常感謝給@ MM8和@Fruchtzwerg!
那麼簡單的事... :)

回答

2

你需要設置你的結合PropertyChanged更新屬性UpdateSourceTrigger bevore的焦點消失,如:

<TextBox Text="{Binding Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

默認情況下,TextBox的綁定文本屬性中的焦點消失後更新。此時切換選項卡意味着更改丟失。設置UpdateSourceTriggerPropertyChanged在更改字符後直接更新屬性。

+0

你是對的!非常感謝! :) –

2

嘗試綁定的UpdateSourceTrigger設置爲PropertyChanged

<TextBox Text="{Binding Val1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="20 10"/> 

這應該強制TextBox失去焦點之前得到立即設置屬性。默認值是LostFocus

+0

你說得對!非常感謝! :) –