wpf 文本框双向绑定不触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18083860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:22:42  来源:igfitidea点击:

Textbox two way binding not triggering

wpfdata-bindingtabcontrolinotifypropertychanged

提问by sprocket12

I have a tab control with 3 objects, 2 lists and a textbox. The text box is bound two way :

我有一个包含 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>

Backing object :

支持对象:

public string CurrentInput
{
    get
    {
        return _currentInput;
    }
    set
    {
        if (value != _currentInput)
        {
            _currentInput = value;
            OnPropertyChanged();
        }
    }
}

Problem is, when I change the text and click another tab it does not update the backing field (does not even hit the setter), however if I change then click the listbox it does...

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

Any reason for this odd behaviour?

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

采纳答案by H.B.

That is not an odd behaviour and has been asked multiple times before. Read about Binding.UpdateSourceTrigger, also see the remarks of the respective propertyyou bind.

这不是一个奇怪的行为,之前已经被问过多次。阅读 about Binding.UpdateSourceTrigger,另请参阅您绑定的相应属性的备注。

回答by Tuan

I've solve this problem (Twoway Binding) by manual trigger the databinding engine using

我已经通过使用手动触发数据绑定引擎解决了这个问题(双向绑定)

DataContext = this;