簡體   English   中英

WPF,TabControl和綁定

[英]WPF, TabControl and Binding

在放置在TabControl中的TextBox控件中,我得到了一個奇怪的行為。
當我更改TextBox控件的值並且不更改焦點時,在更改選項卡上,我松開了此TextBox中的更改。
當我更改焦點並更改選項卡時-所有內容均已保存。

請參閱示例gif:

例子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!
很簡單的事情... :)

嘗試將綁定的UpdateSourceTrigger設置為PropertyChanged

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

這將強制在TextBox失去焦點之前立即設置屬性。 默認值為LostFocus

您需要將綁定的UpdateSourceTrigger設置為PropertyChanged以便在失去焦點之前更新屬性,例如:

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

默認情況下,焦點丟失后,將更新TextBox的綁定text屬性。 此時切換選項卡意味着更改丟失。 UpdateSourceTrigger設置為PropertyChanged在更改字符后立即更新屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM