文本框双向绑定不触发

时间:2013-08-06 15:04:45

标签: wpf data-binding tabcontrol inotifypropertychanged

我有一个标签控件,包含3个对象,2个列表和一个文本框。文本框以两种方式绑定:

<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();
        }
    }
}

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

出现这种奇怪行为的原因是什么?

2 个答案:

答案 0 :(得分:3)

这不是一种奇怪的行为,之前曾被多次询问过。阅读Binding.UpdateSourceTrigger,另请参阅绑定the respective property的评论。

答案 1 :(得分:0)

我通过手动触发数据绑定引擎解决了这个问题(Twoway绑定)

DataContext = this;