JTIM
JTIM

Reputation: 2771

Two-way Binding error in longlistselector, with own datatemplate and class

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

Upvotes: 0

Views: 223

Answers (1)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

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>

Upvotes: 0

Related Questions