Textbox two way binding not triggering

10,520

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

Share:
10,520
sprocket12
Author by

sprocket12

Updated on June 07, 2022

Comments

  • sprocket12
    sprocket12 about 2 years

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

    <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?

  • sprocket12
    sprocket12 almost 11 years
    I opologise for my ignorance but "When used in data-binding scenarios, this property uses the default update behavior of UpdateSourceTrigger.LostFocus." -- when I click another tab does it not "Lose Focus" and thus should trigger an update?
  • H.B.
    H.B. almost 11 years
    @MuhammadA: Not necessarily, WPF has focus scopes, if every tab has its own scope then the TextBox has still focus. It could also be possible that the control is removed before it can lose focus.
  • sprocket12
    sprocket12 almost 11 years
    Yes and that is more complicated problem than whatever else has been asked multiple times. Thanks for your help.
  • H.B.
    H.B. almost 11 years
    @MuhammadA: There are questions relating to that, especially when using DataGrids in TabControls. You should be able to handle some event on the TabControl to move focus before switching tabs.
  • H.B.
    H.B. almost 11 years
    @MuhammadA: Also if you cannot devise your own solution you should not only ask for the reason of the behaviour, which basically is the update triggering.
  • sprocket12
    sprocket12 almost 11 years
    Why not? It definately is strange that the tab click does not trigger a lost focus, I am pretty sure it did in winforms, now I am new to WPF so its only reasonable I think it is odd.
  • H.B.
    H.B. almost 11 years
    @MuhammadA: I was just saying that i only gave the reason because you asked specifically for that.
  • Sombriks
    Sombriks almost 8 years
    hello, would be nice if you provide more context for this sole line of code. The question have a solid context, i'm sure if you elaborate this answer you'll will help much more people.