Question

Question:

Simply, What is the best/most simple way for pre-defined items in a Listbox

(Such as "Bob")

to be populated into a TextBox In a Windows 7 Phone Enviroment?

Example:

If I select "Bob" from the list box - then "Bob" should then be displayed in the text box instantly

(so the user doesn't need to type in a Username)

..and they can instead use a pre-made username from the list of Usernames.

Problems:

  • I've tried looking for these little things and found no solutions.
  • The solutions I did find are incredibly vague.
  • Many videos on YouTube and elsewhere haven't really covered this for Windows 7
  • I fear Windows 8 Mobile is different.
  • Right now, my code fails to populate the Textbox with the Listbox Selection.
  • Relatively new programmer, trying to find my feet in c#

Code:

I don't really know how to do this. I've tried using a built in feature called SelectionChanged but to no success.

Was it helpful?

Solution

Your Class

public class Movie
{
   public string Actor { get; set; }
   public string Name { get; set; }

}

Create a List of Movie with the Data .

public class MovieList : List<Movie>
{
   public MovieList()
   {
     Add(new Movie { Name = "Titanic", Actor = "xxx", });
     Add(new Movie { Name = "LordOFtheRings", Actor = "yyyy",  });
   }
}

In XAML,

<ListBox Height="596" Name="listBox1" Width="380" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

In your code behind.cs

 public MainPage()
{
   InitializeComponent();
   FillListBox();
}

private void FillListBox()
{
   listBox1.ItemsSource = new MovieList();
}

Your Textbox should be

<TextBox Text="{Binding ElementName=listBox1, Path=SelectedValue}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top