繁体   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