coder
coder

Reputation: 4313

How to make 2 way binding in Windows Phone 8.1 ListView

I have a ListView with textBox in one of the columns. User input on the textbox supposed to update the underlying data, that's what my requirement is, but it's not happening. I tried a work around as shown all the way below, it seems to work but I am guessing that the binding itself should take care of it without the work around.

XAML:

<ListView x:Name="listView" Grid.Row="1" ItemsSource="{Binding}" SelectionMode="Multiple">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border Background="{Binding BackGround}">
                    <Grid x:Name="row">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="tblDescription" 
                               Grid.Row="0"
                               Grid.Column="0"
                               Margin="1"
                               TextWrapping="Wrap"
                               Text="{Binding Description}" 
                               FontSize="{StaticResource TextStyleMediumFontSize}"
                               Foreground="Black"/>
                        <TextBlock x:Name="tblItemNumber" 
                               Grid.Row="0"
                               Grid.Column="1"
                               Margin="1"
                               Text="{Binding ItemNumber}" 
                               FontSize="{StaticResource TextStyleMediumFontSize}"
                               FontStyle="Italic"
                               Foreground="Gray"/>
                        <TextBox x:Name="tbQuantity" 
                               Grid.Row="1"
                               Grid.Column="1"
                               Margin="1"
                               Text="{Binding Quantity}" 
                               IsEnabled="{Binding IsEnabled}"
                               FontSize="{StaticResource TextStyleLargeFontSize}"
                               BorderBrush="DarkGray"
                               Foreground="Black"
                               InputScope="Number"
                               Tag="{Binding RowNumber}"
                               TextChanged="tbQuantity_TextChanged"
                               >
                            <TextBox.Header>Quantity</TextBox.Header>
                        </TextBox>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

DataRow class:

public class DataRowBase : INotifyPropertyChanged
{
    #region Events
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    #region Properties
    public int RowNumber { get; set; }
    public string ItemNumber { get; set; }
    public string Description { get; set; }

    private string _Quantity;
    public string Quantity
    {
        get { return _Quantity; }
        set {
            _Quantity = value;
            RaisePropertyChanged("Quantity");
        }
    }
    #endregion
}

Work Around:

private void tbQuantity_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox tb = (TextBox)sender;
    int rownumber = tb.Tag.ToString().ToInt();
    UpdateQuantity(rownumber, value: tb.Text);
}

private void UpdateQuantity(int rownumber, string value)
{
    try
    {
        DataRow datarow = OriginalSource.Where(o => o.RowNumber == rownumber).FirstOrDefault();
        if (datarow != null)
        {
            datarow.Quantity = value;
        }
    }
    catch (Exception ex)
    {

    }
}

Upvotes: 0

Views: 195

Answers (1)

Romasz
Romasz

Reputation: 29792

The default Binding is OneWay, for a TwoWay you will have to declare it, for example:

Text="{Binding ItemNumber, Mode=TwoWay}"

Upvotes: 1

Related Questions