Search code examples
c#xamlwindows-phone-8.1windows-phone

How to populate textboxes-Windows Phone 8.1


What I am trying to do is basically populate textboxes with item names in one xaml (Basket) when selecting item from another xaml(Menu). My code is below :

Menu.xaml

In here when I click a button I want it to automaticallly appear in a textbox in the Basket.xaml.

    public class PassedData
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    private void Vanbtn_Click(object sender, RoutedEventArgs e)
    {


        Frame.Navigate(typeof(Basket), new PassedData { Name = "Cheese" });
    }

    private void LCbtn_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Basket), new PassedData { Name = "Crisp" });
    }

Basket.xaml

       protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
    /**    ItemChosentxt.Text = data.Name;
        ItemChosentxt2.Text = data.Name;
        ItemChosentxt3.Text = data.Name;
        ItemChosentxt4.Text = data.Name;**/


    }

The commented out part adds the name of the item selected. instead of just adding to one textboxit adds to all of the textboxes. i.e. If i select the vanilla btn in Menu.xaml the Name appears in all 4 textboxes instead of just one. So I decided to implement code in each textboxes:

private void ItemChosentxt_TextChanged(object sender, TextChangedEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
        ItemChosentxt.Text = data.Name;
    }

    private void SecItem_TextChanged(object sender, TextChangedEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;
        SecItem.Text = data.Name;
    }

This displays an error underneath the e.Parameter the error code is -

Severity    Code    Description Project File    Line    Suppression State
    Error   CS0103  The name 'data' does not exist in the current context    

and

    Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'TextChangedEventArgs' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'TextChangedEventArgs' could be found (are you missing a using directive or an assembly reference?) 

I am developing a shopping app so e.g. if user selects "Vanilla" and "Cake" from menu how can I make the name be displayed in the Basket textboxes? I am new to app development and apologise for the long question.


Solution

  • The problem is you aren't supplying TextChangedEventArgs e with PassedData.

    The compiler is specifically yelling at you

    1. for saying e.Paramenter (TextChangedEventArgs doesn't have a "Parameter" member)
    2. for using "data", which is undefined because of 1.


    However, I recommend a completely different approach. Just use a listview (and data binding).
    Example:

    Basket.xaml

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding PassedData}" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <TextBox Grid.Column="0" Text="{Binding Name}" />
                        <TextBox Grid.Column="1" Text="{Binding Value}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

    Basket.xaml.cs

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
        // namespace ...
    
    public sealed partial class Basket : Page
    {
        private static ObservableCollection<Menu.PassedData> passedData = new ObservableCollection<Menu.PassedData>();
        public ObservableCollection<Menu.PassedData> PassedData
        {
            get { return passedData; }
            set { passedData = value; }
        }
    
    
        /*Events*/
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        /*Methods*/
    
        /// Fires PropertyChangedEventHandler, for bindables
        protected void OnPropertyChanged(string name)
        {
            var ev = PropertyChanged;
            if (ev != null)
            {
                ev(this, new PropertyChangedEventArgs(name));
            }
        }
    
        public Basket()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Menu.PassedData data = e.Parameter as Menu.PassedData;
            /**    ItemChosentxt.Text = data.Name;
                ItemChosentxt2.Text = data.Name;
                ItemChosentxt3.Text = data.Name;
                ItemChosentxt4.Text = data.Name;**/
            if (data != null)
            {
                PassedData.Add(data);
            }
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(Menu));
        }
    }
    

    }