0
votes

Hey Embarrassingly I have not been able to get two-way binding working from the examples I have found. Here is one of my dataTemplates.

     <local:ChatRoomTemplateSelector.YourSelf>
                            <DataTemplate x:Name="YourSelf">
                                <StackPanel>
                                    <Grid x:Name="YourSelfGrid">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <UserControl Grid.Row="0">
                                            <Path Data="" Fill="LawnGreen" StrokeThickness="0" Stretch="Fill" UseLayoutRounding="True"/>
                                        </UserControl>
                                        <TextBox Text="{Binding Path=Post}" IsReadOnly="{Binding readOnly}" FontSize="20" Margin="39,10" TextWrapping="Wrap" TextAlignment="Center"/>

                                    </Grid>
                                    <StackPanel Orientation="Horizontal">

                                        <Path Data="" Fill="LawnGreen" StrokeThickness="0" Stretch="Fill" UseLayoutRounding="True" Margin="50,-1,0,0"/>

                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="25" TextWrapping="Wrap"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </local:ChatRoomTemplateSelector.YourSelf> 

The element I need to have two-way binding on is Text="{Binding Path=Post}". But cannot get it working. The longlistselector I have, has the itemsource set to:

     System.Collections.ObjectModel.ObservableCollection<Data> source = new System.Collections.ObjectModel.ObservableCollection<Data>();

This source is then set to my Longlistselectors itemsource.

Where the Data class is:

     public class Data : INotifyPropertyChanged
{
    public string Name 
    {
        get;
        set;
    }
    private string _dl;
    public string Post
    {
        get { return _dl; }
        set
        {
            if (value != _dl)
            {
                _dl = value;
                NotifyPropertyChanged("Post");
            }
        }
    }

    public string User
    {
        get;
        set;
    }

    public bool readOnly
    {
        get;
        set;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

This is What I have tried with no success. Hope you can help me with the tweak that gives me two-way binding for the Post, in Data.

Extra Info

I now tried the mode=TwoWay, and finally got that working. Code updated.

But now the update is only happening upon lostfocus. But I need the update of the variable to happen when text is inserted.

So I need to set UpdateSourceTrigger.

But what should this be set to?

Solution

     <TextBox TextChanged="OnTextBoxTextChanged"
     Text="{Binding MyText, Mode=TwoWay,
            UpdateSourceTrigger=Explicit}" />

UpdateSourceTrigger = Explicit is a smart bonus here. What is it? Explicit: Updates the binding source only when you call the UpdateSource method. It saves you one extra binding set when the user leaves the TextBox.

In C#:

     private void OnTextBoxTextChanged( object sender, TextChangedEventArgs e )
      {
       TextBox textBox = sender as TextBox;
        // Update the binding source
        BindingExpression bindingExpr = textBox.GetBindingExpression( TextBox.TextProperty );
        bindingExpr.UpdateSource();
      }:
1
Why not just have Text="{Binding Post}"? - ChrisF
I did this but no use, then I found a post where they put Path, since I did not see the use, I just Tried. But with no help. So just Post is enough. - JTIM
Assuming that the readOnly property is binding OK, then you don't need the Path= part. You probably do need Mode=TwoWay though. - ChrisF
readOnly works, and post worked. Before I needed two-way. Now when I need that I can't get it to work even when I am using Mode=TwoWay. Any Idea on the basis of the code? - JTIM

1 Answers

0
votes

If you want your TextBox to be two way binded, then you should use Text="{Binding Path=Post,Mode=TwoWay}"

Windows phone don't support UpdateSourceTrigger=PropertyChanged but you can find a way to do that here .
Or an alternative is to use the UpdateTextBindingOnPropertyChanged from the Prism library. You can just download the source code of the file I linked and set it in in xaml like this:

<TextBox ...>
    <i:Interaction.Behaviors>
         <prism:UpdateTextBindingOnPropertyChanged/>
    </i:Interaction.Behaviors>
</TextBox>