如何在 Windows Phone 8.1 ListView 中进行双向绑定

How to make 2 way binding in Windows Phone 8.1 ListView

我有一个 ListView,其中一列中有文本框。文本框上的用户输入应该更新基础数据,这就是我的要求,但它没有发生。我一直尝试如下所示的解决方法,它似乎有效,但我猜测绑定本身应该在没有解决方法的情况下解决它。

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>

数据行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
}

解决方法:

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)
    {

    }
}

默认的 BindingOneWay,对于 TwoWay 你必须声明它,例如:

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