简体   繁体   English

在 TabControl 中切换 Tab 时,TextBox 绑定不起作用

[英]TextBox Binding doesn't work when switching Tab in TabControl

I have a TextBox in a TabControl.我在 TabControl 中有一个 TextBox。 If I edit the text in the box and then switch to another tab, the text is lost.如果我编辑框中的文本,然后切换到另一个选项卡,文本就会丢失。 If I change focus (via TAB key on keyboard) and then switch to another tab, the new text is set in my viewmodel.如果我更改焦点(通过键盘上的 TAB 键)然后切换到另一个选项卡,新文本将在我的视图模型中设置。

Here is my code:这是我的代码:

<Window x:Class="TabSwitchProblem.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">
<TabControl ItemsSource="{Binding Pages}">
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TextBox Text="{Binding PageContent}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

public partial class MainWindow : Window
{
    public ObservableCollection<PageViewModel> Pages
    {
        get { return (ObservableCollection<PageViewModel>)GetValue(PagesProperty); }
        set { SetValue(PagesProperty, value); }
    }
    public static readonly DependencyProperty PagesProperty =
        DependencyProperty.Register("Pages", typeof(ObservableCollection<PageViewModel>), typeof(MainWindow), new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        Pages = new ObservableCollection<PageViewModel>();
        Pages.Add(new PageViewModel());
        Pages.Add(new PageViewModel());
        DataContext = this;
    }
}
public class PageViewModel : DependencyObject
{
    public string PageContent
    {
        get { return (string)GetValue(PageContentProperty); }
        set { SetValue(PageContentProperty, value); }
    }
    public static readonly DependencyProperty PageContentProperty =
        DependencyProperty.Register("PageContent", typeof(string), typeof(PageViewModel), new PropertyMetadata(null));
}

How can I be sure to get the text updated in my viewmodel?如何确保在我的视图模型中更新文本?

You may need to add UpdateSourceTrigger=LostFocus to the <TextBox Text="{Binding PageContent}" /> line.您可能需要将UpdateSourceTrigger=LostFocus添加到<TextBox Text="{Binding PageContent}" />行。

Code should look like this代码应该是这样的

<TextBox Text="{Binding PageContent, UpdateSourceTrigger=LostFocus}" />

That should work.那应该工作。

You should set the UpdateSourceTrigger to PropertyChanged if you want your binding to update the target every time the value changes.如果您希望每次值更改时绑定都更新目标,则应将UpdateSourceTrigger设置为PropertyChanged By default the UpdateSourceTrigger for Text property of a TextBox is LostFocus , which updates the target only after the focus is lost.默认情况下, TextBox Text属性的UpdateSourceTriggerLostFocus ,它仅在失去焦点后更新目标。

<TextBox Text="{Binding PageContent, UpdateSourceTrigger=PropertyChanged}" />

The previously accepted answer , although it works, involves changing the binding behavior of the textbox to UpdatesourceTrigger=PropertyChanged .先前接受的答案虽然有效,但涉及将文本框的绑定行为更改为UpdatesourceTrigger=PropertyChanged This may not be acceptable for some usages of textbox or other input-accepting controls.对于文本框或其他输入接受控件的某些用法,这可能是不可接受的。

A simple fix for this is to manually set focus to another element on your control (or the tabcontrol itself) in code-behind on SelectionChanged of your TabControl.对此的一个简单解决方法是在 TabControl 的SelectionChanged的代码隐藏中手动将焦点设置到控件上的另一个元素(或 tabcontrol 本身)。 That way the currently focused input element actually loses focus, triggering the binding:这样当前聚焦的输入元素实际上失去了焦点,触发了绑定:

<TabControl x:Name="MyTabControl" SelectionChanged="MyTabControl_OnSelectionChanged">
private void MyTabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MyTabControl.Focus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM