Question

I've created a listbox and filled it with data by binding it to the list I've created from XML file. I made it in such way, that every single entry is in form of a button.

Now I want to transfer the "Description" content of the button (upon clicking on it) to another listbox, with an addition of the current date. Is there an easy way to do it? Here is my XAML:

<ListBox Height="395" HorizontalAlignment="Left" Margin="27,195,0,0" Name="listBox1" VerticalAlignment="Top" Width="410" AllowDrop="False" ItemsSource="{Binding activities}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Height="150" Width="400" Click="Act_Click">
                <Grid>
                    <TextBlock Text="{Binding Type}" TextAlignment="Left"/>
                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
                </Grid>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<ListBox Height="491" HorizontalAlignment="Left" Margin="10,105,0,0" Name="listBox2" VerticalAlignment="Top" Width="436" ItemsSource="{Binding activities2}">
         <ListBox.ItemTemplate>
             <DataTemplate>
                <Grid>
                  <TextBlock Text="{Binding activities2}" TextWrapping="Wrap" TextAlignment="Left"/>
                </Grid>                               
             </DataTemplate>
         </ListBox.ItemTemplate>
</ListBox>

Here is how I read my file from XML:

namespace ReadXMLfromFile
{
   public class Activity
{
    public string Type { get; private set; }
    public string Description { get; private set; }

    public Activity(string type, string description)
    {
         Type = type;
         Description = description;
    }

    public static List<Activity> LoadActivities(XDocument doc)
    {
        return doc .Root
                   .Elements("Activity")
                   .Select(el => new Activity((string)el.Attribute("Type"),
                                            el.Value))
                   .ToList();                 
    }

    public static List<Activity> LoadActivities()
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists("Activities.xml"))
            {
                 using (var stream = store.OpenFile("Activities.xml",
                                                    FileMode.Open))
                 {
                     return LoadActivities(XDocument.Load(stream));
                 }
            }
        }
        // No user file... load the default activities
        return LoadActivities(XDocument.Load("Assets/Activities.xml"));        
    }
}
}
Was it helpful?

Solution

Simple as that:

var activities = (List<Activity>)listbox1.ItemsSource;
var myObject = (Activity)listbox1.SelectedItem;
activities.Remove(listbox1.SelectedItem);
myObject.Date = DateTime.Now; //to provide your last request(add the current date)
var activities2 = (List<Activity>)listbox2.ItemsSource;
if(activities2 == null)
{ 
    activities2 = new List<Activity>(); 
    listbox2.ItemsSource = activities2;
}
activities2.Add(myObject);

To be sure that the ListBox update their items do

listbox1.ItemsSource = null;
listbox1.ItemsSource = activities;

and

listbox2.ItemsSource = null;
listbox2.ItemsSource = activities2;

or simply use ObservableCollection<Activity> instead of List<Activity> and the ListBoxes should automatically update their items.

EDIT: fixed ItemTemplate for second ListBox

<ListBox Height="395" HorizontalAlignment="Left" Margin="27,195,0,0" Name="listBox1" VerticalAlignment="Top" Width="410" AllowDrop="False" ItemsSource="{Binding activities}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Height="150" Width="400" Click="Act_Click">
                <Grid>
                    <TextBlock Text="{Binding Type}" TextAlignment="Left"/>
                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
                </Grid>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<ListBox Height="491" HorizontalAlignment="Left" Margin="10,105,0,0" Name="listBox2" VerticalAlignment="Top" Width="436" ItemsSource="{Binding activities2}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Height="150" Width="400">
                <Grid>
                    <TextBlock Text="{Binding Type}" TextAlignment="Left"/>
                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
                </Grid>
            </Button>                          
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

OTHER TIPS

I'd suggest binding the SelectedItem on your Listbox to a instance of an activity and when that property changes check to see if the description associated with that item has already been added to your second list and if not, then add it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top