longListSelector get index - c#

I have longListSelector:
<DataTemplate x:Key="AddrBookItemTemplate" >
<StackPanel VerticalAlignment="Top">
<Grid Background="#3FCDCDCD" Margin="3,3,3,3">
<Image Source ="{Binding UrlImage}" Height="100" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" Stretch="Fill"/>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Margin="110,0,0,0" Foreground="Black" FontFamily="Portable User Interface"/>
</Grid>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<phone:LongListSelector
x:Name="AddrBook"
Background="Transparent"
ItemTemplate="{StaticResource AddrBookItemTemplate}"
IsGroupingEnabled="true"
HideEmptyGroups ="true"
SelectionChanged="AddrBook_SelectionChanged"
/>
Preview - name of my class
How can i get index of PressedItem?
What should i write here -
private void AddrBook_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ }

you should write
var longlistselector = (sender as LongListSelector);
int index = longlistselector.DataSource.IndexOf(longlistselector.SelectedItem);

Related

Listbox focus reset after selecting any item

I have a listbox in my WPF project filled by a MYSql database. When I scroll anywhere in the listbox and select an item, it immediately jumps to the first item and show the list from the beginning. I tried this solution but it gives me NullException. Here is the code:
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var addedItems = e.AddedItems;
var selectedItem = (object)null;
if (addedItems.Count > 0)
{
selectedItem = addedItems[0];
}
object id = (((System.Data.DataRowView)selectedItem).Row.ItemArray[0]);
object name = (((System.Data.DataRowView)selectedItem).Row.ItemArray[1]);
object selectedType = (((System.Data.DataRowView)selectedItem).Row.ItemArray[2]);
}
and xaml part:
<ScrollViewer Width="582.5" Name="scrollViewer" HorizontalAlignment="Center" Background="Transparent" VerticalAlignment="Bottom" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden" >
<ListBox x:Name="listBox" SelectionMode="Single" Visibility="Hidden" IsEnabled="False" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Center" Background="Transparent" BorderBrush="Transparent" SelectionChanged="listBox_SelectionChanged" ItemsSource="{Binding Source={StaticResource TypeTable}}" HorizontalContentAlignment="Center" RenderTransformOrigin="0.5,0.5" Padding="0,0,0,90" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stackPanel2" CanHorizontallyScroll="True" Orientation="Horizontal" HorizontalAlignment="Right" Height="180" Width="180" >
<Image Margin="3" Source="{Binding pic_path}" RenderOptions.BitmapScalingMode="Fant" RenderOptions.EdgeMode="Aliased"/>
<TextBox Margin="3" Text="{Binding name}" Visibility="Visible"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>

ListBox WPF Multiselect Not working

I am trying to select multiple options from a listbox. I even tried using Multiple Selection mode but it doesnot work for this specific listbox but works for others. ListBox name is listBox. I am adding the data to that list dynamically in OrderWindow class and i have implemented the selection_changed method but i am unable to select multiple items and i have also binded a list to this listbox.
Here is the code:
namespace ACW2
{
/// <summary>
/// Interaction logic for OrderWindow.xaml
/// </summary>
public partial class OrderWindow : Window
{
List<MenuClass> PizzaInStock = new List<MenuClass>();
List<InvClass> inventory = MainWindow.ItemList;
List<InvClass> pizzatoppings = new List<InvClass>();
List<String> Mainlist = new List<String>();
String addpizzas = "";
double pizzaprice = 0;
private void extratoppingslist(String name)
{
pizzatoppings = new List<InvClass>();
var pizza = MainWindow.MenuList.FirstOrDefault(x => x.Name == name);
List<Ingredient> originaltoppings = pizza.Ingredients;
int originaltoppingsize = pizza.Ingredients.Count;
if (originaltoppingsize >= 5)
{
return;
}
var allpizzatoppings = from a in inventory
where a.Category == "pizza"
select a;
foreach (InvClass item in allpizzatoppings)
{
int exists = 0;
if (originaltoppingsize == 5)
{
break;
}
foreach(Ingredient ing in originaltoppings)
{
if (item.Name.Equals(ing.Name))
{
exists = 1;
break;
}
}
if (exists == 0)
{
pizzatoppings.Add(item);
}
++originaltoppingsize;
}
listBox.ItemsSource = null;
listBox.ItemsSource = pizzatoppings;
}
private void updatepizzaprice()
{
pizzaprice = 0;
if (comboBox1.SelectedItem != null&& comboBox.SelectedItem != null)
{
String pizza = comboBox.SelectedItem.ToString();
String size = comboBox1.SelectedItem.ToString();
extratoppingslist(pizza);
var iteminlists = MainWindow.MenuList.FirstOrDefault(x => (x.Name == pizza) && (x.Size == size));
pizzaprice += (Convert.ToDouble(iteminlists.Price));
List<Ingredient> list = iteminlists.Ingredients;
if (checkBox.IsChecked == true)
{
foreach(Ingredient iss in list)
{
Trace.WriteLine(iss.Name);
}
var item = list.FirstOrDefault(x => x.Name == " dough");
var ingr= inventory.FirstOrDefault(x => x.Name == " dough");
pizzaprice += ((Convert.ToDouble(item.Quantity) * 0.1))*ingr.Price;
var items = list.FirstOrDefault(x => x.Name == " mozzarella");
var ingrs = inventory.FirstOrDefault(x => x.Name == " mozzarella");
pizzaprice += ((Convert.ToDouble(items.Quantity) * 0.15)) * ingrs.Price;
}
List<InvClass> toppings = listBox.SelectedItems.Cast<InvClass>().ToList();
foreach (InvClass top in toppings)
{
var item = inventory.FirstOrDefault(x => x.Name == top.Name);
Trace.WriteLine(top.Name);
if (iteminlists.Size == " regular")
{
pizzaprice += item.Price * 0.25;
}
else if (iteminlists.Size == " large")
{
pizzaprice += item.Price * 0.35;
}
else if (iteminlists.Size == " extra-large")
{
pizzaprice += item.Price * 0.75;
}
}
textBox.Text = "£ " + pizzaprice.ToString();
}
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
updatepizzaprice();
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
updatepizzaprice();
}
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
updatepizzaprice();
}
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
updatepizzaprice();
}
private void checkBox_unchecked(object sender, RoutedEventArgs e)
{
updatepizzaprice();
}
private void checkbox1_unchecked(object sender, RoutedEventArgs e)
{
updateburgerprice();
}
private void checkbox2_unchecked(object sender, RoutedEventArgs e)
{
updateburgerprice();
}
}
}
Here is the xml code:
<Window x:Class="ACW2.OrderWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ACW2"
mc:Ignorable="d"
Title="OrderWindow" SizeToContent="WidthAndHeight">
<StackPanel Orientation="Vertical">
<Label x:Name="label10" Content="Create Order" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" FontSize="24"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<Label x:Name="label4_Copy" Content="Order Summary" Margin="5" FontSize="16"/>
<ListBox x:Name="listBox2" Height="160" Margin="5" MinWidth="180"/>
<Button x:Name="button4" Content="Remove Item" Margin="5" Width="75"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label13" Content="Ingredient Cost (£)" Margin="5" VerticalAlignment="Top"/>
<TextBox x:Name="textBox4" Height="24" Margin="5" TextWrapping="Wrap" Text="£" Width="80"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label12" Content="Net Profit (£)" Margin="5"/>
<TextBox x:Name="textBox5" Height="24" Margin="5" TextWrapping="Wrap" Text="£" Width="80"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label11" Content="Total Price(£)" Margin="5"/>
<TextBox x:Name="textBox1" Height="24" Margin="5" TextWrapping="Wrap" Text="£" Width="80"/>
</StackPanel>
<Button x:Name="button3" Content="Complete Order" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<Label x:Name="label4" Content="1. Pizzas" Margin="5" FontSize="16"/>
<Border BorderBrush="Gray" BorderThickness="1" Margin="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label1" Content="Pizza" Margin="5" />
<ComboBox x:Name="comboBox" Margin="5" Width="120" SelectionChanged="comboBox_SelectionChanged"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label" Content="Size" Margin="5"/>
<ComboBox x:Name="comboBox1" Margin="5" Width="120" SelectionChanged="comboBox1_SelectionChanged"/>
</StackPanel>
<CheckBox x:Name="checkBox" Content="Stuffed Crust" Margin="5" HorizontalAlignment="Center" Checked="checkBox_Checked" Unchecked="checkBox_unchecked"/>
<Label x:Name="label14" Content="Extra Toppings" Margin="5,5,5,0" HorizontalAlignment="Center"/>
<ListBox x:Name="listBox" Height="120" Margin="5,0,5,5" MinWidth="140" HorizontalAlignment="Center" SelectionChanged="listBox_SelectionChanged" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate x:Name="Lists">
<TextBlock>
<Run Text="{Binding Name}"/>
<Run Text=" , "/>
<Run Text="{Binding Price}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label9" Content="Price (£)" Margin="5"/>
<TextBox x:Name="textBox" Height="24" Margin="5" TextWrapping="Wrap" Text="£" VerticalAlignment="Top" Width="80"/>
</StackPanel>
<Button x:Name="button" Content="Add to Order" Margin="5" Width="80" HorizontalAlignment="Right"/>
</StackPanel>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<Label x:Name="label5" Content="2. Burgers" Margin="5" FontSize="16"/>
<Border BorderBrush="Gray" BorderThickness="1" Margin="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label2" Content="Burger" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top"/>
<ComboBox x:Name="comboBox2" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" Width="107" SelectionChanged="comboBox2_SelectionChanged"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label3" Content="Size" Margin="5" VerticalAlignment="Center"/>
<RadioButton x:Name="radioButton" Content="1/4lb" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Center" Checked="radioButton_Checked"/>
<RadioButton x:Name="radioButton1" Content="1/2lb" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Center" Checked="radioButton1_Checked"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
<CheckBox x:Name="checkBox1" Content="Salad" Margin="2" Checked="checkBox1_Checked" Unchecked="checkbox1_unchecked"/>
<CheckBox x:Name="checkBox2" Content="Cheese" Margin="2" Checked="checkBox2_Checked" Unchecked="checkbox2_unchecked"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Label x:Name="label8" Content="Price (£)" HorizontalAlignment="Left" Margin="5" />
<TextBox x:Name="textBox2" Height="24" Margin="5" TextWrapping="Wrap" Text="£" Width="80"/>
</StackPanel>
<Button x:Name="button1" Content="Add to Order" Margin="5" Width="80" HorizontalAlignment="Right" Click="button1_Click"/>
</StackPanel>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<Label x:Name="label6" Content="3. Sundries" Margin="5" FontSize="16"/>
<Border BorderBrush="Gray" BorderThickness="1" Margin="5">
<StackPanel Orientation="Vertical">
<ListBox x:Name="listBox1" Height="120" MinWidth="140" Margin="5" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate x:Name="Lists11">
<TextBlock Background="{Binding Color}">
<Run Text="{Binding Name}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Orientation="Horizontal">
<Label x:Name="label7" Content="Price (£)" Margin="5" VerticalAlignment="Top"/>
<TextBox x:Name="textBox3" Height="24" Margin="5" TextWrapping="Wrap" Text="£" Width="80"/>
</StackPanel>
<Button x:Name="button2" Content="Add to Order" HorizontalAlignment="Right" Margin="5" Width="80" Click="button2_Click"/>
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
</StackPanel>
</Window>

How to access a specific item in itemscontrol and retrieve some data in UWP

I have an ItemsControl with DataTemplate in my Page.Xaml and the code is like below:
<ItemsControl x:Name="chatUI" VerticalAlignment="Bottom">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="myGrid" Width="340" Background="{Binding Background}" HorizontalAlignment="{Binding GridHorizontalAlign}" Margin="10,0,10,10" MinHeight="45" BorderBrush="#FF003A4F" BorderThickness="0,0,0,2">
<Polygon Visibility="{Binding RightVisibility}" Fill="{Binding Background}" Points="0,0 5,6, 0,12" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,-5,0" />
<Polygon Visibility="{Binding LeftVisibility}" Fill="{Binding Background}" Points="5,0 0,6, 5,12" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-5,0,0,0" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="15" FontFamily="Segoe UI" Foreground="White" Margin="10,10,10,0"/>
<TextBlock Grid.Row="1" Text="{Binding Time}" TextWrapping="Wrap" FontSize="11" FontFamily="Segoe UI" Foreground="LightGray" Margin="10,0,10,5" VerticalAlignment="Bottom" TextAlignment="Right"/>
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
What I need right now is getting Text which is bound to the TextBlock when I right click on the grid named myGrid. How is that possible in C#?
We can add the RightTapped event of the Grid, it will be fired when you right click the Grid.
In the RightTapped event we can use Grid.Children to get the collection of child elements of the Grid. That we can get the Grid in the root Grid that named myGrid. That we can use the Grid.Children to get the TextBlock in the Grid.
For example:
private async void myGrid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
var RightTapGrid = sender as Grid;
var childernElements = RightTapGrid.Children;
foreach (var item in childernElements)
{
var grid = item as Grid;
if (grid != null)
{
var itemchildernElements = grid.Children;
foreach (var text in itemchildernElements)
{
var textBlock = text as TextBlock;
var dialog = new ContentDialog()
{
Title = textBlock.Text,
MaxWidth = this.ActualWidth
};
dialog.PrimaryButtonText = "OK";
dialog.SecondaryButtonText = "Cancel";
await dialog.ShowAsync();
break;
}
}
}
}
If you get your Binding Data from Class called ClassName
You can try this code
XAML:
<ListView x:Name="chatUI" VerticalAlignment="Bottom" SelectionChanged="chatUI_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="myGrid" Width="340" Background="{Binding Background}" HorizontalAlignment="{Binding GridHorizontalAlign}" Margin="10,0,10,10" MinHeight="45" BorderBrush="#FF003A4F" BorderThickness="0,0,0,2">
<Polygon Visibility="{Binding RightVisibility}" Fill="{Binding Background}" Points="0,0 5,6, 0,12" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,-5,0" />
<Polygon Visibility="{Binding LeftVisibility}" Fill="{Binding Background}" Points="5,0 0,6, 5,12" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-5,0,0,0" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="15" FontFamily="Segoe UI" Foreground="White" Margin="10,10,10,0"/>
<TextBlock Grid.Row="1" Text="{Binding Time}" TextWrapping="Wrap" FontSize="11" FontFamily="Segoe UI" Foreground="LightGray" Margin="10,0,10,5" VerticalAlignment="Bottom" TextAlignment="Right"/>
</Grid>
</Grid>
</DataTemplate>
</Listview.ItemTemplate>
And add SelectionChanged Event :
private void chatUI_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView view = (ListView)sender;
//Get Selected Item
ClassName class = view.SelectedItem as ClassName;
string path = class.Text;
// Now we have Text of selected item in Listview
}

Image Gallery with Flipview

I want to create a gallery image on the first page contains a thumbnail for that category and when thumbnail is selected, it will open the image and description of the selected image in flipview (can be swipe to the right and to the left when thumbnail is selected for the image before and after). I have difficulties when applying it into flipview.
Code:
MainPage XAML
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Grid.RowSpan="3"
Padding="116,137,40,46"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick" Background="#FF6996D1" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="240" Height="180">
<Border>
<Image Source="{Binding ImagePath}" Stretch="Uniform" AutomationProperties.Name="{Binding Title}"/>
</Border>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid GroupPadding="0,0,70,0"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
MainPage:
public MainPage()
{
this.InitializeComponent();
Gallery();
}
private async void Gallery()
{
var sampleDataGroups = await DataItemSource.GetGroupsAsync();
this.DefaultViewModel["Groups"] = sampleDataGroups;
}
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the appropriate destination page, configuring the new page
// by passing required information as a navigation parameter
var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}
ItemDetailPage XAML:
<Grid Grid.Row="1" x:Name="contentRegion" Background="#FF6996D1">
<Image Source="{Binding ImagePath}" HorizontalAlignment="Left" Height="559" Margin="84,20,0,49" VerticalAlignment="Center" Width="732"/>
<ScrollViewer x:Name="myScroll" VerticalScrollBarVisibility="Auto" Margin="852,60,50,91" VerticalScrollMode="Auto" HorizontalScrollBarVisibility="Auto">
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" Height="2210" Width="425" FontSize="27" TextAlignment="Justify" />
</ScrollViewer>
</Grid>
ItemDetailPage Code:
public ItemDetailPage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
}
private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data
var item = await DataItemSource.GetItemAsync((String)e.NavigationParameter);
this.DefaultViewModel["Item"] = item;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
navigationHelper.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
navigationHelper.OnNavigatedFrom(e);
}
How do I apply flipview on ItemDetailPage?
Note:
For more code detail you can view the sample
To apply flipview on ItemDetailPage, we can add FlipView under "contentRegion" and set the Image and ScrollViewer as FlipView's ItemTemplate like following:
<Grid x:Name="contentRegion" Grid.Row="1" Background="#FF6996D1">
<FlipView ItemsSource="{Binding Group.Items}" SelectedItem="{Binding Item, Mode=TwoWay}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Width="732"
Height="559"
Margin="84,20,0,49"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="{Binding ImagePath}" />
<ScrollViewer x:Name="myScroll"
Margin="852,60,50,91"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
VerticalScrollMode="Auto">
<TextBlock Width="425"
Height="2210"
FontSize="27"
Text="{Binding Description}"
TextAlignment="Justify"
TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
And in code-behind, set the data source like following:
private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data
var item = await DataItemSource.GetItemAsync((String)e.NavigationParameter);
var group = await DataItemSource.GetGroupByItemAsync(item);
this.DefaultViewModel["Group"] = group;
this.DefaultViewModel["Item"] = item;
}
Here I add a GetGroupByItemAsync(SampleDataItem item) method in DataItemSource which can retrieve the group according to the item.
public static async Task<SampleDataGroup> GetGroupByItemAsync(SampleDataItem item)
{
await _DataItemSource.GetSampleDataAsync();
// Simple linear search is acceptable for small data sets
var matches = _DataItemSource.Groups.Where(group => group.Items.Contains(item));
if (matches.Count() == 1) return matches.First();
return null;
}
Besides these, we also need to remove DataContext="{Binding Item}" form root Grid and put in in <Grid Background="#FF6996D1" DataContext="{Binding Item}">.
After this, the FlipView should be able to work. However here is a strange behavior, if we select the second or third image, the previous image in flip view won't show like following
We are investigating on this issue. As a workaround, we can disable FlipView's virtualization by changing its ItemsPanel like:
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
The complete XAML code of ItemDetailPage might like:
<Page x:Class="ImageGalerry.ItemDetailPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="using:ImageGalerry.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:data="using:ImageGalerry.Data"
xmlns:local="using:ImageGalerry"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding DefaultViewModel,
RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">
<Grid d:DataContext="{Binding Groups[0].Items[0], Source={d:DesignData Source=/DataModel/DataItem.json, Type=data:DataItemSource}}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="140" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Back button and page title -->
<Grid Background="#FF6996D1" DataContext="{Binding Item}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1366" />
</Grid.ColumnDefinitions>
<Button x:Name="backButton"
Margin="39,59,0,0"
VerticalAlignment="Top"
AutomationProperties.AutomationId="BackButton"
AutomationProperties.ItemType="Navigation Button"
AutomationProperties.Name="Back"
Command="{Binding NavigationHelper.GoBackCommand,
ElementName=pageRoot}"
Style="{StaticResource NavigationBackButtonNormalStyle}" />
<TextBlock x:Name="pageTitle"
Margin="120,40,30,40"
VerticalAlignment="Top"
IsHitTestVisible="false"
Style="{StaticResource HeaderTextBlockStyle}"
Text="{Binding Title}"
TextWrapping="NoWrap" />
<!--<MediaElement x:Name="mediaplayer" Source="images/ost.mp3" AudioCategory="BackgroundCapableMedia" />
<Button x:Name="PlayButton" Content="Stop" Click="PlayButton_Click" Margin="1274,72,0,30" />-->
</Grid>
<Grid x:Name="contentRegion" Grid.Row="1" Background="#FF6996D1">
<FlipView ItemsSource="{Binding Group.Items}" SelectedItem="{Binding Item, Mode=TwoWay}">
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Width="732"
Height="559"
Margin="84,20,0,49"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="{Binding ImagePath}" />
<ScrollViewer x:Name="myScroll"
Margin="852,60,50,91"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
VerticalScrollMode="Auto">
<TextBlock Width="425"
Height="2210"
FontSize="27"
Text="{Binding Description}"
TextAlignment="Justify"
TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
</Grid>
</Page>
Please note that disabling UI virtualization for FlipView may negatively impact performance especially when there are a lot of images. If you have a lot of images, you can try to use incremental loading and data virtualization.

How to get CheckBox "Ticked" inside ListBox on run time

I am creating windows phone 8 application, I have checkbox inside listbox, how do I get checkbox "CHECKED" when I click on checkbox?
I tried my best but not getting the result?
see my code below:
<ListBox x:Name="listBox1" Width="429" Height="621" HorizontalAlignment="Left" Margin="21,43,0,59" VerticalAlignment="Top" ItemsSource="{Binding}" SelectedItem="{Binding}" SelectionChanged="listBox1_SelectionChanged" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Vertical" Width="440">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Favorite" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" FontWeight="SemiBold" Margin="344,-35,9,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="Auto" Width="Auto" TextAlignment="Left" FontWeight="SemiBold"/>
</StackPanel>
<CheckBox x:Name="CheckBox1" Height="72" Foreground="Black" IsChecked="False" Margin="353,-40,28,0" BorderBrush="Black" Loaded="CheckBox1_Loaded" Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is my code behind file:
private void CheckBox1_Checked(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
cb.IsChecked = true;
}
I tried this on emulator in my listbox and it works fine. The textboxes checked and unchecked without any problem.
<ListBox.BorderBrush>
<SolidColorBrush />
</ListBox.BorderBrush>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="0,1,0,0" Width="600" Padding="0">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Stretch="UniformToFill" Source="{Binding Image}" Width="130" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image.Clip>
<RectangleGeometry RadiusX="15" RadiusY="15" Rect="0,0,130,130" />
</Image.Clip>
</Image>
<--CheckBox x:Name="CheckBox1" Height="72" Foreground="Black" IsChecked="False" BorderBrush="Black"/-->
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I got the solution:
private void CheckBox1_Checked(object sender, RoutedEventArgs e)
{
CONTACTS data = (sender as CheckBox).DataContext as CONTACTS;
string contactId = data.contactId;
string isFav = data.isFavourite.ToString();
}
Here is the answer:
private void CheckBox1_Checked(object sender, RoutedEventArgs e)
{
CLASS_NAME item = (sender as CheckBox).DataContext as CLASS_NAME;
string id = item.Id;
string name = item.Name;
}

Categories