none
Clear textbox after adding item MVVM RRS feed

  • Question

  • Hello,

    How do I clear the textboxes after typing in some text and sending it?

    This is using the MVVM-pattern having no access to the codebehind.

    MyCar.xaml

     <HubSection ContentTemplate="{StaticResource CarCT}" />

    App.xaml (with contenttample name CarCT)

    <ListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding SomeID}" Foreground="White" /> <TextBlock Text="{Binding SomeCar}" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate>

    <StackPanel Grid.Row="1">
                <TextBlock Text="id" />
                <TextBox Text="{Binding NewCar.SomeID, Mode=TwoWay}" />
                <TextBlock Text="Carname" />
                <TextBox Text="{Binding NewCar.SomeCar, Mode=TwoWay}" />
                <Button Content="Save Car" Click="Button_Click" />

            </StackPanel>


    CarVewModel.xaml.cs

      

           private ObservableCollection<MyModel> _roundUpData = new ObservableCollection<MyModel>();
            public ObservableCollection<MyModel> RoundUpData
            {
                get { return _roundUpData; }
                set
                {
                    if (value == _roundUpData)
                        return;
                    _roundUpData = value;
                }
            }

            private MyModel newCar = new MyModel();

            public MyModel NewCar
            {
                get { return newCar; }
                set { newCar = value; }
            }


    Thursday, July 24, 2014 1:14 PM

Answers

  • NewCar needs to fire a change notification. As is the binding system can't know anything changed and so doesn't update.
    • Marked as answer by coffeebakery Sunday, July 27, 2014 4:23 PM
    Friday, July 25, 2014 4:35 PM

All replies

  • I'm not sure what you mean by "after typing some text and sending it".

    You update the TextBlock by updating the data it is bound to.

    That data needs to implement INotifyPropertyChanged. If your NewCar property is changed it needs to fire a PropertyChanged notification to let the binding system know to update.

    --Rob

    Thursday, July 24, 2014 8:27 PM
  • Sorry, obviously I mean the values in my textboxes, Newcar.

    Back in the old Days I would simply name my textbox something and after typing in a string and pressing a button to make what I want with it just clear the textbox with "Mytextbox.Text = string.empty();" or something.

    I have uploaded a exampleproject on OneDrive.

    Type in a new car and press newcar.

    http://1drv.ms/1pilLo5

    Also please note, in my actual project I have all xaml-code in app.xaml as in my original post. 

    Friday, July 25, 2014 5:30 AM
  • What "textboxes"?
    Friday, July 25, 2014 6:12 AM
  • What "textboxes"?

    Hi,

    properties that is inserted to the Observablecollection. (NewCar.SomeID) How would I go about clearing the values inserted there after an update to the observablecollection?

    <StackPanel Grid.Row="1">
                <TextBlock Text="id" />
                <TextBox Text="{Binding NewCar.SomeID, Mode=TwoWay}" />
                <TextBlock Text="Carname" />
                <TextBox Text="{Binding NewCar.SomeCar, Mode=TwoWay}" />
                <Button Content="Save Car" Click="Button_Click" />
    
            </StackPanel>

    As I wrote you can also download the example source-Project.

    Friday, July 25, 2014 11:15 AM
  • You clear the underlying model, the UI will reflect the change

    http://pauliom.wordpress.com

    Friday, July 25, 2014 11:18 AM
  • I tried that but it doesn't work.

                NewCar = new MyModel();

    Friday, July 25, 2014 4:24 PM
  • NewCar needs to fire a change notification. As is the binding system can't know anything changed and so doesn't update.
    • Marked as answer by coffeebakery Sunday, July 27, 2014 4:23 PM
    Friday, July 25, 2014 4:35 PM
  • Hey PKR2000 for some reason I can't see your "reply, quote, mark as answer" on your post but yours was the first right one.

    But I mark Robs as the answer at the moment!

    Sunday, July 27, 2014 4:24 PM