Binding listbox in listbox on Windows Phone 8 - windows-phone-8

I want to bind AnswerList in QuestionList. When I run code, only have question list on the screen.
<ListBox x:Name="listques" ItemsSource="{Binding QuestionList }">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="quesdetail" Text="{Binding QuestionContent.que_detail}" HorizontalAlignment="Left" Margin="27.669,34.338,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="252.564" Width="419.534" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto">
</TextBlock>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="ansdetail" Foreground="Blue" Text="{Binding Answer.ans_detail}">
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

You should have a property AnswerList inside your Question object. Assign that as the itemsSource of the inner ListBox. So for each question you will have a list of answers.
<ListBox x:Name="InnerListBox" ItemsSource = "{Binding QuestionContent.AnswerList}">

#Bells is right. To elaborate his answer a little more, let me explain by an external example.
Example:-
// MainPage.xaml page
// eg. we have a nested ListBox for questions and answers
<ListBox x:Name="lbxRoot">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding que}" FontSize="30" />
<ListBox ItemsSource="{Binding lstAns}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" Margin="20 0 0 0" FontSize="30"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
To assign ItemsSource to the RootListBox let's define one model class which contains one question and a list of answers of that question.
// MainPage.xaml.cs file add one class named Model as below
public class Model
{
public string que { get; set; }
public List<string> lstAns { get; set; }
}
Now, we are going to assign ItemsSource to root ListBox in Loaded event of the page so declare the event handler in the MainPage's Constructor.
this.Loaded += MainPage_Loaded;
and then define the Loaded event handler as below.
// MainPage.xaml.cs file
// Loaded event handler
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Model m1 = new Model()
{
que = "Question 1",
lstAns = new List<string>()
{
"que 1 - ans 1", "que 1 - ans 2", "que 1 - ans 3"
}
};
Model m2 = new Model()
{
que = "Question 2",
lstAns = new List<string>()
{
"que 2 - ans 1", "que 2 - ans 2", "que 3 - ans 3"
}
};
List<Model> lstModels = new List<Model>();
lstModels.Add(m1);
lstModels.Add(m2);
lbxRoot.ItemsSource = lstModels;
}
It will give output like below image:
And there you go..!!
Try to relate your scenario with this example, and you're done..!
Hope that helps..

A ListBoxItem will try to find the binding inside the properties of its current item in the ItemSource. So the first ListBox will try to find the properties on its ItemSource and the inner ListBox will try to find its bindings on its ItemSource.
If you want to bind the inner ListBox's ListBoxItem you have to specify the relativeSource (To make it clear prop1 means a property, no matter which one):
<ListBox ItemSource="{Binding list1}"
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding propList1}"/>
<ListBox ItemsSource="{Binding list2}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding DataContext.propList1,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
With this line
<TextBlock Text="{Binding DataContext.propList1,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>
wpf will look for a parent of type ListBox in its visualtree, and use its DataContext, and its DataContext is the ListBoxItem of the outter ListBox.

Related

How to pass CommandParameter on holed item?

How to pass CommandParameter on holed item?
my xaml code
<ListView Grid.Row="1" Name="ProfilesListView" SelectedItem="{Binding SelectedUser,Mode=TwoWay}" ItemsSource="{Binding Path=ViewAllProfile,Mode=TwoWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" HorizontalAlignment="Center">
</ItemsWrapGrid>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<I:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Holding">
<core:InvokeCommandAction Command="{Binding Path=HoldingCommand}" CommandParameter="{Binding ElementName=ProfilesListView,Path=SelectedItem}" ></core:InvokeCommandAction>
</core:EventTriggerBehavior>
</I:Interaction.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10,0,0,0">
<Image Margin="10" Source="{Binding ImageURL}" Width="150" Height="150" >
</Image>
<TextBlock x:Name="userName" Text="{Binding MenuName}" Foreground="White" HorizontalAlignment="Center"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
my viewmodel is .i am not getting the holding item details .how to solve this
_HoldingCommand = new RelayCommand<object>(HoldedUser);
Not sure this can be done directly through MVVM binding.
The holding event won't set the selected item straight away...
If you would register to the holding event and use code behind, you'll notice the 'holded' item is only known in the HoldingRoutedEventArgs - something like this:
private async void OnListViewHolding(object sender, HoldingRoutedEventArgs e)
{
var selectedItemThroughHolding = e.OriginalSource.DataContext as ***Model;
}
So my suggestion would be to use this code behind event and reroute the selectedItem to the viewmodel there...

How to handler click item from string inside of listview Windows Phone 8.1

I've been reading this article about Navigation Drawer and i Succeed, but how can i create a Click Event to each item i have inside of mu ListView?
i have that Array and bind all this to listview!
string[] menuItems = new string[5] { "Item1", "Item2", "Item3", "Item4", "Item5" };
ListMenuItems.ItemsSource = menuItems.ToList();
XAML.....
<Grid x:Name="ListFragment" Background="#F4F4F4">
<ListView x:Name="ListMenuItems" SelectedItem="true" SelectionChanged="ListMenuItems_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="18" Foreground="Black" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Thanks!
Define an event handler for ItemClick event of ListView. You also need to set IsItemClickEnabled property to true.
<ListView x:Name="listview"
IsItemClickEnabled="True"
ItemClick="listView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
..
..
private void listView_ItemClick(object sender, ItemClickEventArgs e) {
// Code here
}

Windows Phone 8, finding a checkbox item inside listbox

I have a checkbox inside a stackpanel inside a listbox. I need to set the visibility of that checkbox based on some logical operations, so I need to find that element in listbox.
I am using the following code to find the checkbox on my PageLoad, after the list`s dataSource has been set.
But on this listboxItem i get a null value and a exception saying : reference is not a valid visual dependencyobject
private void searchListCheckBoxVisibility()
//private void SearchList_Loaded(object sender, RoutedEventArgs e)
{
try
{
ListBoxItem listItem = this.SearchList.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem;
CheckBox targetCheckBox = FindFirstElementInVisualTree<CheckBox>(listItem);
}
}
The following is my xaml :
Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Margin="13,10,7,10" x:Name="SearchList" DoubleTap="SearchList_DoubleTap">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="searchListPanel" Orientation="Horizontal" Width="400" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Width="340">
<StackPanel Orientation="Vertical">
<TextBlock Text="Name : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding User}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Project : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding Project}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Status : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Date : " FontWeight="Bold" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
<TextBlock Text="{Binding DT_On_OffBoarded}" TextWrapping="Wrap" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
</StackPanel>
<StackPanel>
<TextBlock Text=""/>
</StackPanel>
</StackPanel>
<CheckBox Width="60"
x:Name="user_Checkbox"
Content="" Tag="{Binding ID}"
Visibility="Collapsed"
Checked="user_Checkbox_Checked"
Unchecked="user_Checkbox_UnChecked"
Style="{StaticResource ResourceKey=MSFTGuidelinesCheckBox}"
IsChecked="False"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I am stuck at this issue for a while, and it has been getting onto my nerves now, Please help.
Thanks in advance.
You could simply use the VisualTreeHelper to find a control within your Listbox data template.
Reference: How to access a specific item in a Listbox with DataTemplate?

How to play a song on clicking on list of songs?

I have a media player project for my course. I have a list songs:
<ListBox Name="listBoxSongs" SelectionChanged="listBoxSongs_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="100" Margin="8" Source="{Binding Image}"/>
<StackPanel >
<TextBlock Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title}" TextWrapping="Wrap" FontSize="24" />
<TextBlock VerticalAlignment="Center" Text="{Binding Artist}" TextWrapping="Wrap"/>
</StackPanel>
<TextBlock Margin="10" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding Duration}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The code below uses media library and shows list of songs with listview:
foreach (Song song in songs)
{
if (song != null)
mySongs.Add(new MySongs()
{
Artist = song.Artist.ToString(),
Title = song.Name,
Duration = (new DateTime(song.Duration.Ticks)).ToString("mm:ss")
});
else
{
TextBlock msg = new TextBlock();
msg.Text = "no music";
listBoxSongs.Items.Add(msg);
}
if (song.Album.HasArt)
{
BitmapImage img = new BitmapImage();
img.SetSource(song.Album.GetAlbumArt());
mySongs.Last().Image = img;
}
else
mySongs.Last().Image = new BitmapImage(new Uri("Images/noArt.png", UriKind.Relative));
}
listBoxSongs.ItemsSource = mySongs;
How to navigate to "mainPage.xaml" and play a song from my list when I click on listview Item?

How to filter an observable collection in Windows Phone 8?

I tried this How to: Filter Data in a View so i tried this approach but it didn't work for me because windows phone 8 environment couldn't find 'ListCollectionView'.
My collection is following.
//My observableCollection
ObservableCollection<ClonedWrapper> dsForProgress = new ObservableCollection<ClonedWrapper >();
//My Function which shows at first time.
private async Task CloudImages()
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
try
{
List<MainWrapper> serverdata = new List<MainWrappe>();
serverdata = await ImageUrlAsync();//This function is returning the Deserialize object of images
if (serverdata.Count != 0)
{
foreach (var imgInfo in serverdata)
{
string folderPath = "Fagbokforlaget/Books/" + BookInfo.Id.Trim();
ClonedWrapper item = new ClonedWrapper()
{
//Name, Cover, Info, Title, IsEnableButton, IsVisibleButton,
// IsVisible are the assigned keys to the xaml page
Id = imgInfo.Id,
Name = imgInfo.Name,
Cover = await GetCoverImage(imgInfo),
Info = imgInfo.Info,
Title = imgInfo.Title,
Url = imgInfo.Url,
IsEnableButton = "True",
IsVisible = "Collapsed",
Date = Convert.ToDateTime(imgInfo.Date)
};
if (isf.DirectoryExists(folderPath))
{
item.ButtonStatus = "Read";
item.IsVisibleBookDeleteButton = "Visible";
}
else
{
item.ButtonStatus = "Download";
item.IsVisibleBookDeleteButton = "Collapsed";
}
dsForProgress.Add(item);
}
}
else
{
MessageBox.Show("You have no any downloaded books!");
}
listCloudImages.ItemsSource = dsForProgress;
}
catch (Exception exe)//I am getting exception here on extreme first run in absense of internet.
{
MessageBox.Show(exe.Message);
}
}
//My xaml page is following. It is the default page that loaded at first time. you can say it is the face of the application. User download the images. and buttonStatus convert into the Read button. so now what i am doing i will keep two buttons in appliationbar one cloud second downloaded. ON Download button click i want to show only downloaded images.
<ListBox Name="listCloudImages" Visibility="Visible" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Auto" FontFamily="Segoe UI" FontStyle="Normal" FontWeight="Thin" Margin="0,0,0,50">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<tools:WrapPanel Orientation="Horizontal">
</tools:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Background="#151414" CornerRadius="3" Margin="3" Width="150" TextOptions.DisplayColorEmoji="True" BorderBrush="#1c1b1b">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image x:Name="imgBookImage" Source="{Binding Cover}" Visibility="Visible" VerticalAlignment="Top" HorizontalAlignment="Center"
Width="80" Height="100" ImageOpened="imgBookImage_ImageOpened"/>
<StackPanel Orientation="Horizontal">
<TextBlock Visibility="{Binding IsVisible}" Text="{Binding ProgressPercentage}" FontFamily="Segoe UI" FontSize="18" FontWeight="ExtraBold" Foreground="White" Margin="5,0"></TextBlock>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontFamily="Segoe UI" FontSize="13.5"
Foreground="White" TextTrimming="WordEllipsis"
VerticalAlignment="Top" HorizontalAlignment="Left"
TextWrapping="Wrap"
Width="300" Padding="2"/>
<TextBlock Text="{Binding Info}" FontSize="13.5" FontFamily="Segoe UI"
Foreground="White"
VerticalAlignment="Top" HorizontalAlignment="Left"
TextWrapping="Wrap"
Width="300" Padding="2"/>
<ProgressBar x:Name="downloadProgressBar" Foreground="Green" IsIndeterminate="True" VerticalAlignment="Center" Width="120" TextOptions.TextHintingMode="Animated" Visibility="{Binding IsVisible}" CharacterSpacing="2"/>
<Button Content="{Binding ButtonStatus}" x:Name="btnDownload" IsEnabled="{Binding IsEnableButton, Converter={StaticResource ButtonVisibilityIsEnableConverter}}"
Click="btnDownload_Click" Tag="{Binding}" Width="120" BorderThickness="1" FontSize="13.5" Margin="0,5"
FontFamily="Segoe UI" tools:SlideInEffect.LineIndex="2" HorizontalAlignment="Left" VerticalAlignment="Top"
Foreground="White">
</Button>
<Image x:Name="imgCancelImage" Source="/Assets/Tiles/CancelImage.png" Visibility="{Binding IsVisible}" VerticalAlignment="Center" Margin="97,-66,0,0"
Tap="imgCancelImage_Tap" HorizontalAlignment="Right" Width="25" Height="25" Tag="{Binding}"/>
<Button x:Name="btnDeleteImage" Click="btnDeleteImage_Click"
Tag="{Binding}" BorderThickness="1" Margin="97,-66,0,0"
Height="55" Width="55"
Visibility="{Binding ButtonStatus, Converter={StaticResource DeleteButtonVisibilityConverter}}">
<Button.Background>
<ImageBrush ImageSource="/Images/delete.png" Stretch="Fill"></ImageBrush>
</Button.Background>
</Button>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Based on your code, you can do something quite easy, if you wanna show only downloaded books try this:
private void ApplicationBarButtonShowAllDownLoaded(object sender, EventArgs e)
{
var c = (Application.Current as App).dsForProgress.Where(x => x.ButtonStatus.Equals("Download"));
listCloudImages.ItemsSource = c;
}
be sure that ObservableCollection<ClonedWrapper> dsForProgress is public
In your book model, you can pass a bool property IsDownload and when you download books you set this property to true.For your filter, just use linQ for take all books download or no dowload like this :
var noDownloadedBooks =
from b in yourObservableCollection
where b.IsDownload == false
select b;
var downloadedBooks =
from b in yourObservableCollection
where b.IsDownload == true
select b;
What you need to do is use a CollectionViewSource, this can point to your original collection and it can handle filtering!
Scott Hanselman has a great example blog post about it here... http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx