I'm working on Windows Phone 8.1 RT project and i want to get my binding item when mapcontrolSP tapped.
<Maps:MapControl x:Name="Map" MapServiceToken="abcdef-abcdefghijklmno">
<Maps:MapItemsControl x:Name="mapitem" ItemsSource="{Binding}">
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="mapcontrolSP" Orientation="Horizontal" Background="Red" Tapped="ItemStckPanel">
<Image Source="Assets/ico-venue.png" Height="45" Width="45"
Maps:MapControl.Location="{Binding Geopoint}"
Maps:MapControl.NormalizedAnchorPoint="{Binding Anchor}"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">
<TextBlock FontSize="15" Foreground="White" Text="{Binding name}"/>
<TextBlock FontSize="15" Foreground="White" Text="{Binding address}"/>
<TextBlock FontSize="15" Foreground="White" Text="{Binding distance}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
</Maps:MapControl>
I create a tapped event on the StackPanel element but item is null:
private void ItemStckPanel(object sender, TappedRoutedEventArgs e)
{
var item = sender as FsqBasicItem;
MessageDialog dialog = new MessageDialog(item.name);
dialog.ShowAsync();
}
How can i get item ?
Fast solution:
private void ItemStckPanel(object sender, TappedRoutedEventArgs e)
{
var stackPanel = sender as StackPanel;
var item = stackPanel.DataContext as FsqBasicItem;
if(item != null)
{
MessageDialog dialog = new MessageDialog(item.name);
dialog.ShowAsync();
}
}
Related
I want to change the background of StackPanel when clicking TextBlock.
enter image description here
now background is bule! I want to change it!
<TreeView Name="tvView" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}">
<Grid VerticalAlignment="Top" Margin="0">
<StackPanel Orientation="Horizontal" d:LayoutOverrides="Height">
<TextBlock Name="ConfidenceLevelReminderText" Text="!" FontWeight="Bold" HorizontalAlignment="Center" Foreground="#FF0000" Width="{Binding Path=ConfidenceLevelWidth}" Margin="0,0,0,0"></TextBlock>
<TextBlock TextWrapping="Wrap"
Padding="0,3,0,3"
Text="{Binding Path=Name}"
ToolTip="{Binding Path=NameToolTip}"
Tag="{Binding Path=TagInfo}"
MouseLeftButtonDown="name_MouseLeftButtonDown"
FontSize="14"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="#FF666666"
Margin="0"
Width="{Binding Path=TextWidth}"
Cursor="Hand"/>
<Button x:Name="btnIngnore" Width="{Binding Path=IgnoreWidth}" Click="btn_ignore_Click" Tag="{Binding Path=NodeId}" Margin="0" BorderThickness="0" Background="{x:Null}" Style="{StaticResource ButtonStyle32}" IsEnabled="{Binding Path=IgnoreWidth,Converter={StaticResource itb}}" Cursor="Hand">
<TextBlock Text="xxxx" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" FontSize="12" Foreground="#FF666666"/>
</Button>
</StackPanel>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
If you want
to change the background of StackPanel when clicking TextBlock.
You can add EventSetter to the TextBlock's Style and handle event in the event handler.
<TextBlock Text="...">
<TextBlock.Style>
<Style TargetType="TextBlock">
<EventSetter Event="MouseDown" Handler="Mouse_LBtnDown"/>
</Style>
</TextBlock.Style>
</TextBlock>
private void Mouse_LBtnDown(object sender, MouseButtonEventArgs e)
{
StackPanel stp = null;
var visParent = VisualTreeHelper.GetParent(sender as FrameworkElement);
while (stp == null && visParent != null)
{
stp = visParent as StackPanel;
visParent = VisualTreeHelper.GetParent(visParent);
}
if (stp == null) { return; }
stp.Background = Brushes.Coral;
}
For make it reusable and adjustable you could make a behavior from the event handler.
If you don't want what you have asked, then rethink your question and ask new one.
I have a LongListSelector with some textblocksand images inside.
How can I set the Image's visibility programmatically?
I have them set to collapsed and I want to enable them on selection_changed event of the LongListSelector.
XAML:
<phone:LongListSelector Name="LongListSel" Margin="0,-38,-22,2" ItemsSource="{Binding Items}" SelectionChanged="LongListSelector_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="110" Width="432">
<StackPanel Width="311" Margin="0,0,0,0">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}" />
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
<StackPanel Name="playImage" Height="50" Width="50" Margin="0,0,10,0">
<Image Source="Assets/Tiles/Iconsmind-Outline-Play-Music.ico" Visibility="{Binding ImageVis}" Width="50" Height="50" HorizontalAlignment="Left" Tap="Image_Tap_1"/>
</StackPanel>
<StackPanel Name="downloadImage" Height="50" Width="50" Margin="0,0,0,0">
<Image Source="Assets/Tiles/Download.ico" Visibility="{Binding ImageVis}" Width="50" Height="50" HorizontalAlignment="Left" Tap="Image_Tap_1"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</phone:PanoramaItem>
selection changed event:
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ItemViewModel item = new ItemViewModel();
item.ImageVis = Visibility.Visible;
//it can't be called the way you are doing it of course and it still doesn't work
}
ViewModel
private Visibility _imageVis;
public Visibility ImageVis
{
get { return _imageVis; }
set
{
_imageVis = value;
NotifyPropertyChanged("ImageVis");
}
}
Create a property of type Visibility an bind it to your image visibility.
ViewModel:
private Visibility _DownloadImage;
public Visibility DownloadImage
{
get { return _DownloadImage; }
set
{
if (_DownloadImage != value)
{
_DownloadImage = DownloadImage;
OnPropertyChanged("DownloadImage");
}
}
}
xaml
<Image Source="Assets/Tiles/Download.ico" Visibility="{Binding DownloadImage}" ... />
xaml.cs (I would prefer to bind the command also to the ViewModel, to keep the MVVM pattern)
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DownloadImage = Visibility.Visible;
}
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);
I have a LongListSelector, that store data from my Azure SQL Database.
This is my C# code:
private async void RefreshTodoItemsToday()
{
try
{
coll = await todoTable
.Where(todoItem => todoItem.TpEvt == "today")
.ToCollectionAsync();
}
catch (MobileServiceInvalidOperationException e)
{
MessageBox.Show(e.Message, "Error loading items", MessageBoxButton.OK);
}
ListItemstoday.ItemsSource = coll;
}
And this is my XAML:
<phone:LongListSelector Name="ListItemsToday">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="TxtEvt" Text="{Binding Text}" TextWrapping="Wrap" Foreground="Gray" TextAlignment="Center" FontSize="30" Padding="30">
</TextBlock>
<Line X1="0" Y1="10" X2="240" Y2="10" Stroke="SkyBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="21"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
The registers are stored in my LongListSelector - it is working fine.
Now this is my doubt: How can i read the properties for each register in my LongListSelector? For each register, i have fields such as "Id", "TypeEvent", "Hour", "Date", etc.
So, how can i read each individual value, according by the SelectedItem in LongListSelector?
For example, if i wish to see in a MessageBox the ID from a Selected Item...how can i do this in code?
I tried the following:
var tmp1 = (TodoItem)ListItemsToday.SelectedItem;
var tmp2 = tmp1.Id;
MessageBox.Show(tmp2.ToString());
When i try to cast this code, this is the error i got:
*System.NullReferenceException: Object reference not set to an instance of an object.
at Project.MainPage.ContextMenuRemove_Click(Object sender, EventArgs e)*
Someone can help'me, please?
Thank you friends.
React to selection changed:
<phone:LongListSelector Name="ListItemsToday" SelectionChanged="ListItemsToday_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="TxtEvt" Text="{Binding Text}" TextWrapping="Wrap" Foreground="Gray" TextAlignment="Center" FontSize="30" Padding="30">
</TextBlock>
<Line X1="0" Y1="10" X2="240" Y2="10" Stroke="SkyBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="21"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
with this
private void ListItemsToday_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = ListItemsToday.SelectedItem as TodoItem;
MessageBox.Show(item.Text);
}
Or react to tap event in data template
<phone:LongListSelector Name="ListItemsToday" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Tap="ListItemsToday_Tap">
<TextBlock Name="TxtEvt" Text="{Binding Text}" TextWrapping="Wrap" Foreground="Gray" TextAlignment="Center" FontSize="30" Padding="30">
</TextBlock>
<Line X1="0" Y1="10" X2="240" Y2="10" Stroke="SkyBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="21"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
like this:
private void ListItemsToday_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var item = (sender as FrameworkElement).DataContext as TodoItem;
MessageBox.Show(item.Text);
}
Here is my xaml of the Panorama page item.
<controls:PanoramaItem x:Name="deeln" Header="Deelnemers" Style="{StaticResource subtitle}">
<!--Double line list with image placeholder and text wrapping-->
<ListBox Margin="12,0,-12,0" ItemsSource="{Binding ItemsDeelnemer}" x:Name="lbDeelnemer" SelectionChanged="lbDeelnemer_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<TextBlock Foreground="Black" TextWrapping="Wrap" Text="{Binding LineNr}" Style="{StaticResource PhoneTextExtraLargeStyle}" ></TextBlock>
<StackPanel Width="430" Height="100">
<TextBlock Foreground="Black" TextWrapping="Wrap" Text="{Binding LineNaamWielrenner1}" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="35"></TextBlock>
<TextBlock Foreground="Black" TextWrapping="Wrap" Text="{Binding LineNaamWielrenner2}" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="35"></TextBlock>
</StackPanel>
</StackPanel>
</ScrollViewer>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
Here is my code in the Panorama page.
private void lbDeelnemer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
#region go to specific deelnemerinfo screen
// If selected index is -1 (no selection) do nothing
if (lbDeelnemer.SelectedIndex == -1)
return;
// Navigate to the new page
if (lbDeelnemer.SelectedIndex == 0)
{
NavigationService.Navigate(new Uri("/DeelnemerInfo.xaml", UriKind.Relative));
//NavigationService.Navigate(new Uri("/DeelnemerInfo.xaml?selectedItem=" + lbDeelnemer.SelectedIndex, UriKind.Relative));
}
// Reset selected index to -1 (no selection)
lbDeelnemer.SelectedIndex = -1;
#endregion
}
Here is my code from the non panorama page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//second try
string strItemIndex;
if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))
PanoramaControl.DefaultItem = MyPanorama.Items[Convert.ToInt32(strItemIndex)];
base.OnNavigatedTo(e);
//first try
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.ItemsDeelnemer[index];
}
}
My problem, I want to navigate like you do with a default databound application. You click on the first listitem and you go to a new page (non panorama).
It looks simple but i can't find it.
Try binding the tag attribute to the index/itemvalue
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton Tag="{Binding FileName}" Click="location_Click"/>
<TextBlock Text="{Binding DateCreated}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
navigate accordingly
private void location_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton clicked = (HyperlinkButton)sender;
string uri = "/noteapp;component/ViewEdit.xaml?id=" + clicked.Tag;
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}
and on panaroma page use this something like this to retrieve the value
filename = NavigationContext.QueryString["id"];
var storage = IsolatedStorageFile.GetUserStoreForApplication();
using (var file = storage.OpenFile(filename, FileMode.Open))