0

我显示成员全名列表,当我选择成员时,它将转到详细信息页面。我直接在文本框中编辑信息,然后单击应用程序栏中的保存,但信息在我编辑时没有更改......它仍然是旧值

请帮帮我!

我有这样的成员列表名称

<ListBox x:Name="Listmember" Height="500" SelectionChanged="Listmember_SelectionChanged" ItemsSource="{Binding Data}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="466" Margin="0, 0, 0, 12">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="0"></Grid>
                        <StackPanel Grid.Column="1" >
                            <TextBlock FontSize="40"   Text="{Binding FullName}" Foreground="#FFEA0909" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>


                        </StackPanel>
                        <Grid Grid.Column="2">
                            <Button x:Name="Deletebutton" Height="60" Width="60" Click="deleteButton_Click" BorderBrush="{StaticResource TransparentBrush}">

                                <Image Source="/Assets/delete.dark.png" Visibility="{StaticResource PhoneDarkThemeVisibility}" Margin="-24"/>
                            </Button>
                        </Grid>
                    </Grid>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

和 SelectionChanged 是:

private void Listmember_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // If no customer is selected, just return
        if (Listmember.SelectedItem == null) return;
        // Get the parent application that contains the customer being edited
        App thisApp = Application.Current as App;

        // Set this to the selected customer
        thisApp.SelectedMember = Listmember.SelectedItem as Member;

        // Navigate to the detail page
        NavigationService.Navigate(new Uri("/View/MemberDetail.xaml", UriKind.RelativeOrAbsolute));

    }

在导航到的 MemberDetails.xaml 中:

<Grid Name="ListDetails" Grid.Row="0">
            <TextBlock Grid.Row="0" Name="fn" Text="FullName" FontSize="30" VerticalAlignment="Bottom"/>
            <TextBox Grid.Row="1" Name="fullnameTextBox" Text="{Binding FullName, Mode=TwoWay}" TextWrapping="Wrap"/>
            <TextBlock Grid.Row="2" Name="ad" Text="Address" FontSize="30" VerticalAlignment="Bottom"/>
            <TextBox Grid.Row="3" Name="addressTextBox" Text="{Binding Address, Mode=TwoWay}" TextWrapping="Wrap" />
 </Grid>
 <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton 
            IconUri="/Images/save.png" 
            Text="Save" 
            x:Name="SaveButton" 
            Click="Save_Click"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

和背后的代码

 public MemberDetail()
    {
        InitializeComponent();
        this.DataContext = App.MainViewModel;
    }
    ViewDetails view = new ViewDetails();



    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Get the parent application that contains the active custom
        App thisApp = Application.Current as App;

        // Load the active customer into the viewmodel
        view.LoadDetails(thisApp.SelectedMember);

        // Set the data context to the viewmodel
        ListDetails.DataContext = view;

    }




 private void Save_Click(object sender, EventArgs e)
        {
            App thisApp = Application.Current as App;

           view.UpdateDetails(thisApp.SelectedMember);

            // Go back to the previous page
            NavigationService.GoBack();
        }

和 ViewModel 中的 ViewDetails 类:

public class ViewDetails : INotifyPropertyChanged
{

    private string _fullname;

    public string FullName
    {
        get
        {
            return _fullname;
        }
        set
        {
            _fullname = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Fullname"));
            }
        }
    }

    private string _address;

    public string Address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Address"));
            }
        }
    }

    public void UpdateDetails(Member abc)
    {
        abc.FullName = FullName;
        abc.Address = Address;


    }

    public void LoadDetails(Member abc)
    {
        FullName = abc.FullName;
        Address = abc.Address;
    }



     #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the app that a property has changed.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}
4

1 回答 1

1

Binding Mode Twoway 中的 TextBox 在失去焦点之前不会更新源。

  • 如果您尝试按下按钮,您会发现按下按钮会触发文本框的 LostFocus 事件,因此在您执行按钮中的代码之前,数据将被更新并处于正确状态。

  • 如果您尝试按 ApplicationBarIconButton,您会发现 LostFocus 没有触发。所以数据不会更新,会处于旧状态。

现在我们该如何克服呢?

简单,您可以执行以下解决方案之一,每次文本更改时更新源

  1. 从这个问题使用显式绑定结合 OnTextChanged

    显式:仅在调用 UpdateSource 方法时更新绑定源。当用户离开 TextBox 时,它会为您节省一个额外的绑定集。

    在xml中

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

    在 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();
    }
    
  2. 您可以使用此链接中的行为,该行为侦听 TextChanged 事件并更新源。我更喜欢这个解决方案,因为它很容易添加到您的文本框中,而无需在后面的代码中添加更多行。您需要做的就是将此行添加到您的文本框中。

    <TextBox x:Name="tbx" Text="{Binding Name, Mode=TwoWay}">
    <i:Interaction.Behaviors>
    <local:UpdateSourceOnTextChangedBehavior />
    </i:Interaction.Behaviors>
    </TextBox>
    

    不要忘记引用 System.Windows.Interactivity 还将这些命名空间添加到您的页面命名空间定义中

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
     xmlns:local="name space of the behavior"
    
于 2013-12-27T22:16:17.403 回答