wpf - 文本框双向绑定(bind)未触发

标签 wpf data-binding tabcontrol inotifypropertychanged

我有一个带有 3 个对象、2 个列表和一个文本框的选项卡控件。文本框以两种方式绑定(bind):

<TabControl x:Name="tcTabs" ItemsSource="{Binding Rooms, UpdateSourceTrigger=PropertyChanged}" Margin="5" BorderThickness="1" IsSynchronizedWithCurrentItem="True">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding Name}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="22"/>
                            </Grid.RowDefinitions>

                            <ListBox ItemsSource="{Binding ReceivedMessages}" DisplayMemberPath="Raw" Grid.Row="0" Grid.Column="0" BorderThickness="0" />
                            <ListBox ItemsSource="{Binding Users}" DisplayMemberPath="Nick" Visibility="{Binding Type, Converter={StaticResource UserListVisibilityConverter}}" Grid.Row="0" Grid.Column="1" BorderThickness="1,0,0,0" BorderBrush="#FFBBBBBB" Width="130" />
                            <TextBox Text="{Binding CurrentInput, Mode="TwoWay"}" Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="0,1,0,0" BorderBrush="#FFBBBBBB" Height="22" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

支持对象:
public string CurrentInput
{
    get
    {
        return _currentInput;
    }
    set
    {
        if (value != _currentInput)
        {
            _currentInput = value;
            OnPropertyChanged();
        }
    }
}

问题是,当我更改文本并单击另一个选项卡时,它不会更新支持字段(甚至不会点击 setter),但是如果我更改然后单击它的列表框...

这种奇怪的行为有什么原因吗?

最佳答案

这不是一个奇怪的行为,之前已经被问过多次。阅读 Binding.UpdateSourceTrigger ,另见the respective property的备注你绑定(bind)。

关于wpf - 文本框双向绑定(bind)未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083860/

相关文章:

c# - 使用 `ElementName` 的 WPF 绑定(bind)失败,但在 Path 属性中指定 ElementName 有效吗?

Android 数据绑定(bind)不适用于 <merge> 属性

java - 如何通过数据绑定(bind)更新布局元素的可见性

c# - 如何在 C# 中更改选项卡控件的字体属性而不更改其子控件的字体?

c# - 在 TabControl 中预加载选项卡以加快选项卡切换

c# - 为什么使用静态资源会破坏 Visual Studio 中的设计器?

c# - C# WPF 中的 "Simple MVVM Toolkit"子模型类

c# - wpf mvvm绑定(bind)时间跨度,只显示分钟:seconds?

c# - 如何创建没有选项卡标题的 TabControl?

c# - WPF 组合框所选项目错误 - 显示 "System.Data.Entity.DynamicProxies"