I have a listbox showing name of place, building, x and y from a wcf service. How can I trigger a click from there and return the result to a map.
My Xaml
<ListBox x:Name="Results" Height="450" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0 0 0 20">
<TextBlock Text="{Binding SearchVal}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
<TextBlock Text="{Binding Category}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
<TextBlock Text="{Binding X}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
<TextBlock Text="{Binding Y}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
code behind my click action (Code from other sources)
public void searchResult_Click(object sender, RoutedEventArgs e)
{
//adds the map Point to the map when a results is selected.
var selectedSearchBtn = (Button)sender;
var selectedVal = (string)selectedSearchBtn.Content;
Classes.Global.searchedValue = selectedVal;
for (int i = 0; i < searchVal.Count; i++)
{
if (searchVal[i].Equals(selectedVal))
{
Classes.Global.posx = posx[i];
Classes.Global.posy = posy[i];
}
}
NavigationService.GoBack();
}
How can I use the above code to take the X and Y out ?
public void searchResult_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button; //Assuming your trigger is a button UI element.
if (button != null)
{
var place = button.DataContext as TestMap.Classes.Global.Place;
if (place != null)
{
Classes.Global.posx = place.posx;
Classes.Global.posy = place.posy;
}
}
NavigationService.GoBack();
}
Related
I'm making a timer with adjustable time. For that reason I want to set the time using listboxes (one for minutes and one for seconds). The filling of the listboxes is accomplished using the following code.
public EditTime()
{
this.InitializeComponent();
List<Numbers> numbers = new List<Numbers>();
for (int i = 0; i < 61; i++)
{
numbers.Add(new Numbers() { Number = i });
}
listBoxobjM.ItemsSource = numbers;
listBoxobjS.ItemsSource = numbers;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
}
public class Numbers
{
public int Number { get; set; }
}
In XAML I have the following code for the ListBoxes:
<Grid Grid.Row="1" x:Name="ContentRootM" Margin="65,0,0,0" Width="130">
<ListBox Background="Transparent" Margin="10,10,10,10" BorderThickness="2" MaxHeight="580" Grid.Row="2" x:Name="listBoxobjM" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="5" BorderBrush="{ThemeResource ApplicationHeaderForegroundThemeBrush}" BorderThickness="1" Background="{ThemeResource ButtonBackgroundThemeBrush}">
<TextBlock TextAlignment="Left" HorizontalAlignment="Left" Width="70" Height="80" x:Name="LbM" Text="{Binding Number}" FontSize="48" Foreground="{ThemeResource ButtonForegroundThemeBrush}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid Grid.Row="1" x:Name="ContentRootS" Margin="10,0,0,0" Width="130">
<ListBox Background="Transparent" Margin="10,10,10,10" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="2" x:Name="listBoxobjS" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="5" BorderBrush="{ThemeResource ApplicationHeaderForegroundThemeBrush}" BorderThickness="1" Background="{ThemeResource ButtonBackgroundThemeBrush}">
<StackPanel Width="125" Orientation="Horizontal" Grid.Row="1">
<TextBlock TextAlignment="Left" HorizontalAlignment="Left" Width="70" Height="80" x:Name="LbS" Text="{Binding Number}" TextWrapping="Wrap" FontSize="48" Foreground="{ThemeResource ButtonForegroundThemeBrush}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
When I navigate to this page I can select the preselected time (minutes and seconds) using listBoxobjM.SelectedIndex = i; in the following code. I can scroll manually to this Item and see it is selected.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
string parameter = e.Parameter as string;
string[] parts = parameter.Split(';');
id = int.Parse(parts[0]);
....
DatabaseHelper fetch = new DatabaseHelper();
TeamType team = fetch.GetTeamType(id);
for (int i = 0; i < 60; i++)
{
if (i == team.PlayTime.Minutes)
{
listBoxobjM.SelectedIndex = i;
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);
}
if (i == team.PlayTime.Seconds)
{
listBoxobjS.SelectedIndex = i;
listBoxobjS.ScrollIntoView(listBoxobjS.SelectedIndex);
}
}
}
I want the Listbox automatically to scroll to the SelectedIndex, but instead the Listbox shows the first Items on top. How can I take care theListBox scrolls to the SelectedIndex?
ListBox.ScrollIntoView() takes an item that's in the ListBox, not an index. (MSDN documentation) So instead of
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedIndex);
try
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);
instead.
Eventually I found the solution myself via an other Stack Overflow question. Here they also had problems with the ListView because it was filled with a delay. Using a await Task.Delay (see code) the problem is solved. I thank everybody for his contribution.
for (int i = 0; i < 60; i++)
{
if (i == team.PlayTime.Minutes)
{
listBoxobjM.SelectedIndex = i;
await Task.Delay(1000);
listBoxobjM.ScrollIntoView(listBoxobjM.SelectedItem);
}
if (i == team.PlayTime.Seconds)
{
listBoxobjS.SelectedIndex = i;
await Task.Delay(1000);
listBoxobjS.ScrollIntoView(listBoxobjS.SelectedItem);
}
}
I was making my list more nice by changing my HyperlinkButton style and then my app just stopped working.. The debugger shows nothing to me.
My xaml code:
<ListBox Name="Listing" ItemsSource="{Binding Coletores,Mode=TwoWay}" HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Stretch">
<HyperlinkButton NavigateUri="/OcorrenciasPage.xaml" Name="hlbColetor" Click="hlbColetor_Click">
<HyperlinkButton.Content>
<StackPanel>
<TextBlock Text="{Binding Nome}" FontSize="42"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock FontSize="24" FontWeight="Light">
<Run Text="x:"/>
<Run Text="{Binding Latitude}"/>
</TextBlock>
<TextBlock FontSize="24" FontWeight="Light" Margin="20,0,0,0">
<Run Text="y:"/>
<Run Text="{Binding Longitude}"/>
</TextBlock>
</StackPanel>
</StackPanel>
</HyperlinkButton.Content>
</HyperlinkButton>
<Line Width="*" Stroke="Gray" StrokeThickness="4"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the code behind:
public ColetoresPage()
{
InitializeComponent();
db = new AppDataContext();
DataContext = this;
}
private ObservableCollection<Coletor> _coletor;
public ObservableCollection<Coletor> Coletores
{
get { return _coletor; }
set
{
_coletor = value;
OnPropertyChanged("Coletores");
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Coletores = new ObservableCollection<Coletor>(PessoaAux.coletores);
}
private void hlbColetor_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
Coletor coletor = btn.DataContext as Coletor;
ColetorAux.id = coletor.Id;
ColetorAux.nome = coletor.Nome;
ColetorAux.longitude = coletor.Longitude;
ColetorAux.latitude = coletor.Latitude;
ColetorAux.pessoa = coletor.Pessoa;
}
}
I don't know what is wrong, my code was running normal before some edits but I don't know what edit I done is bugging my code
My page having city listing and searching functionality. When page first time loading, it is showing all record.
When user enter search Text and tap on search button. it is not updating gridview list. I check by placing debug point my code is working fine. but gridview list not showing updated list.
Following is my code.
XAML:
<StackPanel VerticalAlignment="Top">
<TextBlock Style="{StaticResource ListTextBlockStyle}" FontWeight="Bold" Text="{Binding Description}" />
<TextBlock Style="{StaticResource ListTextBlockStyle}" Text="{Binding Description}" />
</StackPanel>
<TextBlock Style="{StaticResource DistanceTextBlockStyle}" TextWrapping="Wrap" Text="XXXm" />
<Image Width="10" VerticalAlignment="Center" Source="ms-appx:///Assets/arrowright.png"/>
</StackPanel>
<Rectangle Height="1" Stroke="Black" StrokeThickness="0.5" Margin="0,3,0,0" />
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<Border Grid.Row="1" Height="60" VerticalAlignment="Bottom">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox x:Name="txtSearch" Margin="0,0,10,0" TextWrapping="Wrap" PlaceholderText="Search" VerticalAlignment="Center" Width="300" Height="50" />
<Image x:Name="imgSearch" Height="50" Width="50" Source="ms-appx:///Assets/btnSearch.png" Tapped="imgSearch_Tapped"/>
</StackPanel>
</StackPanel>
</Border>
C#:
public List<City> gs_CityList = new List<City>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
fillCityList();
}
private void fillCityList()
{
gs_CityList.Clear();
if (string.IsNullOrEmpty(CityListManagerManager.ms_searchTxt))
{
foreach (City foCity in CityListManagerManager.ms_CityList)
{
City loCity = new City();
loCity.Description = foCity.Description.Replace("\n", "").Substring(0, 15) + "...";
loCity.longtitude = foCity.longtitude;
loCity.latitude = foCity.latitude;
loCity.Location = foCity.Location;
gs_CityList.Add(loCity);
}
}
else
{
foreach (City foCity in CityListManagerManager.ms_CityList.Where(p => p.Description.ToLower().Contains(CityListManagerManager.ms_searchTxt.ToLower())))
{
City loCity = new City();
loCity.Description = foCity.Description.Replace("\n", "").Substring(0, 15) + "...";
loCity.longtitude = foCity.longtitude;
loCity.latitude = foCity.latitude;
loCity.Location = foCity.Location;
gs_CityList.Add(loAEDPin);
}
txtSearch.Text = CityListManagerManager.ms_searchTxt;
}
if (gs_CityList.Count > 0)
{
gvCityList.DataContext = gs_CityList; // --- This binding data to gridview
}
else
MessageBox("City not found...!");
}
private void imgSearch_Tapped(object sender, TappedRoutedEventArgs e)
{
CityListManagerManager.ms_searchTxt = txtSearch.Text.Trim();
fillCityList();
}
You should try changing your List<City> into an ObservableCollection<City>, as this allows the binding to get notified about changes.
You could also think about using a CollectionViewSource as data source for the GridView and modifying its Filter property instead of re-filling the collection.
I want to update DownloadingSpeed Property which is binded to TextBox in a ListBoxItem. I am generating ListBoxItems in c#. Please guide me how do i update Binding Content of TextBoxes within a ListItemBox in C#.
WPF LisBox Code
<ListBox Width="Auto"
Name="WebsiteList"
MouseUp="SelectedListItem"
SelectedIndex="0"
Grid.Column="1"
Grid.Row="2"
Grid.RowSpan="2"
Margin="0,0,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="920">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Grid Width="920">
<TextBlock FontWeight="Bold" FontSize="18" Width="Auto">
<Hyperlink NavigateUri="http://google.com" FontStyle="Italic">
<Label Content="{Binding WebsiteTitle}" /><Label FontSize="10" Margin="0,0,0,3" Content="{Binding DirectorySize}" />
</Hyperlink>
</TextBlock>
<TextBlock Width="0" TextAlignment="right">
<TextBlock Visibility="Hidden" Text="{Binding DownloadID}"/>
<TextBlock Visibility="Hidden" Text="{Binding DirectoryPath}"/>
<TextBlock Visibility="Hidden" Text="{Binding CurrentTime}"/>
<TextBlock Visibility="Hidden" Text="{Binding CurrentDate}"/>
<TextBlock Visibility="Hidden" Text="{Binding GivenUrl}"/>
</TextBlock>
</Grid>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Grid Width="920">
<ProgressBar Name="progress1" Maximum="100" Minimum="0" Value="30" Background="#FFF" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Width}" Height="10" />
</Grid>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Grid Width="920">
<TextBlock HorizontalAlignment="Left" Width="Auto">Status: <TextBlock Text="{Binding Status}"/></TextBlock>
<TextBlock Width="Auto" TextAlignment="right">
<TextBlock Text="Downloading Speed: "/>
<TextBlock Name="DownloadingSpeed" Text="{Binding DownloadingSpeed}"/>
</TextBlock>
</Grid>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In MainWindow.xaml.cs
private void GenerateList()
{
List<Download> WebsitesList = new List<Download>();
WebsitesList.Add(new Download() {DownloadID = ID, WebsiteTitle = WebTitle, Status = WebStatus, CurrentTime = CurrentTime,CurrentDate = CurrentDate, DownloadingSpeed = DownloadSpeed, DirectoryPath = path, DirectorySize = helper.GetDirectorySize(path),GivenUrl = url });
WebsiteList.ItemsSource = WebsitesList;
}
//get download speed and update DownloadingSpeed
private void updateDownloadingSpeed(object sender, EventArgs e)
{
interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();
downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;
previousbytesreceived = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;
Download.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s"; //Rounding to 2 decimal places and save in DownloadSpeed Property
}
In Download.cs
public class Download : INotifyPropertyChanged
{
private string _DownloadingSpeed = "0 kb/s";
public string DownloadingSpeed
{
get { return _DownloadingSpeed; }
set
{
if (_DownloadingSpeed == value)
return;
_DownloadingSpeed = value;
this.OnPropertyChanged("DownloadingSpeed");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Add UpdateSourceTrigger for TextBlock.
<TextBlock Name="DownloadingSpeed" Text="{Binding DownloadingSpeed, UpdateSourceTrigger=PropertyChanged}"/>
You might find that your code works if you use WPF properly by defining properties and declaring XAML correctly. It is customary to have an ObservableCollection<T> property that you data bind to the UI collection control. You would then data bind it to the ItemsSource property of the control:
<ListBox ItemsSource="{Binding Items}" ... />
Then, when you want to update any properties of the items, you need to set the properties of the items. This:
Download.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s";
... is not setting the property of an item from the collection. To do that, you'd need to do something like this:
// Use whatever filter you want to find the correct item from the collection
Download downloadInstance = Items.Where(d => d.Name == "SomeName").First();
downloadInstance.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s";
I want to access the text content after binding of the "GameName" textblock that is inside the listbox.
<controls:PanoramaItem Header="games" Margin="0" Height="800" Foreground="White" VerticalAlignment="Center">
<!--Double line list with text wrapping-->
<ListBox x:Name="GamesListBox" Margin="0,0,-12,66" Height="614" ItemsSource="{Binding dataFeed}" SelectionChanged="GamesListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Image Height="100" Width="100" Source="{Binding AllGamesImage}" Margin="12,0,9,0" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill" />
<StackPanel Width="311">
<TextBlock x:Name="GameName" Text="{Binding AllGamesTitle}" TextWrapping="Wrap" Foreground="White" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="0,0,0,1"/>
<TextBlock Text="{Binding AllGamesDescription}" TextWrapping="Wrap" Margin="0,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
i want to take whatever is set as the text content and pass it to another page as a parameter or anything.
code behind after Shawn Kendrot suggestion.
private void GamesListBox_SelectionChanged(object sender, EventArgs e)
{
var myObject = GamesListBox.SelectedItem as NewGamesClass;
string gameName = myObject.TitleCode;
NavigationService.Navigate(new Uri("/Pages/AchivementListPage.xaml?gameName=" + gameName, UriKind.Relative));
}
i get a NullExeception when returing to the page here:
string gameName = myObject.TitleCode;
Why access the Text property when you can access the property of the object?
void GamesListBox_SelectionChanged(object sender, EventArgs e)
{
var myObject = GamesListBox.SelectedItem as MyObject;
string gameName = myObject.AllGamesTitle;
// Do something with gameName
}
Try this
ListBoxItem selItem = (ListBoxItem)(listboxWeight.ItemContainerGenerator.ContainerFromIndex(listboxWeight.SelectedIndex));
StackPanel weightpanel = (StackPanel)selItem.Content;
var child1 = weightpanel.Children[0] as TextBlock;
var child2 = weightpanel.Children[1] as TextBlock;