How to get selected item from AutoSuggestBox in Windows Phone 8.1 - c#

In CS File "SelectedItem" is not working what is the alternative to "SelectedItem" for AutoSuggestBox in WP8.1
In XAML File:
<AutoSuggestBox x:Name="tblkpersonname" Width="380" Margin="0,-7,0,0" ItemsSource="{Binding}" TextChanged="tblkpersonname_TextChanged">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"
Tag="{Binding PersonID}"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
In Cs File:
private void tblkpersonname_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
try
{
if (tblkpersonname.SelectedItem != null)
{
tblkdes.Text = ((values)tblkpersonname.SelectedItem).Description;
persononlineimg.Source = new BitmapImage(new Uri(((values)tblkpersonname.SelectedItem).FlickrPersonImageUrl, UriKind.RelativeOrAbsolute));
}
}
catch (Exception ex)
{
Exceptions.SaveOrSendExceptions("Exception in tblkpersonname_SelectionChanged_1 Method In AddCast.cs file.", ex);
}
}

There's no "SelectedItem" in the AutoSuggestBox provided with Windows Phone 8.1 and neither there's one in the developer tools for Windows 10.
The AutoSuggestBox works like a regular TextBox, the only plus here's the possibility to have a panel/popup that show ups for giving suggestions based on the ItemsSource you passed.
Actually it only works if the ItemsSource is a collection of string, since the DisplayMemberPath doesn't work, at least for me.
So the only way to retrieve the "SelectedItem" you should use the Text property.
I know it's not actually the same, but the AutoSuggestBox it's not a ComboBox.

Xaml
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<AutoSuggestBox
Text="{Binding EnteredAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding AddressAutoComplete}"
ItemTemplate="{StaticResource Autocomplete}"
TextMemberPath="name">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SuggestionChosen">
<core:InvokeCommandAction Command="{Binding TextSearchChangedCommand}" CommandParameter="{Binding this}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
ViewModel (Prism)
TextSearchChangedCommand = new DelegateCommand<Object>((Object) =>
{
method(Object);
});
public void method(Object adr)
{
AutoSuggestBoxSuggestionChosenEventArgs a = (AutoSuggestBoxSuggestionChosenEventArgs)adr;
Address selected = (Address)a.SelectedItem;
}
I spent a whole day to realize it:-)

Related

How do I handle SelectionChange when the ComboBox is

I have a ListBox, where the list element has a ComboBox, a TextBox and a slider. Depending on the selction of the ComboBox either the TextBox or the slider should be visible.
<ListBox Name="lstPWM" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<!-- more definitions -->
</Grid.ColumnDefinitions>
<ComboBox ItemsSource="{Binding Path=Gebertyp, Converter={local1:EnumToCollectionConverter}, Mode=OneTime}"
SelectedValuePath="Value"
DisplayMemberPath="Description"
SelectionChanged="PWMTyp_SelectionChanged"
SelectedValue="{Binding Path=Gebertyp}" />
<TextBox Visibility="{Binding GeberVisible}" Text="{Binding GeberNmr, Mode=TwoWay}"/>
<Slider Visibility="{Binding WertVisible}" Value="{Binding Wert, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The code behind is:
public partial class MainWindow : Window
{
public ObservableCollection<PWMKanal> PWM_col { get; set; } = new();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
lstPWM.ItemsSource = PWM_col;
foreach (var item in Board.PWM) PWM_col.Add(item); //Board.PWM is the data source.
}
private void PWMTyp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox box = sender as ComboBox; // Finding the line in the ListBox.
PWMKanal PWM = box.DataContext as PWMKanal;
int z = PWM_col.IndexOf(PWM);
Board.PWM[z].Gebertyp = (QuellePWM)box.SelectedValue;
if (Board.PWM[z].Gebertyp == QuellePWM.Sender)
{
PWM_col[z].GeberVisible = Visibility.Visible; // I thought that i may change the
PWM_col[z].WertVisible = Visibility.Hidden; // ObservableColelction directly
} // but the display is not updated.
else // In Debug mode i see, that PWM_coll
{ // is changed as expected, but no effect
PWM_col[z].GeberVisible = Visibility.Hidden; // on the GUI.
PWM_col[z].WertVisible = Visibility.Visible;
}
if (PWM_col.Count != 0) // this code is intended to update the GUI, but every time
{ // a new item is added the Selection Change fires again
PWM_col.Clear(); // and i get a stack overflow in an endless loop.
foreach (var item in Board.PWM) PWM_col.Add(item);
}
}
}
The comments describe my approaches and problems:
I change the selected element of the ObservableCollection directly, but this has no effect on GUI. At least tho code doesn't crash.
I clear the list ObservableCollection PWM_col, but then i get an infinite loop: every time an element is added to the list the SelectionChange event fires, calling the routin again. Result is stack overflow.
Now my questions to my approaches:
Is it possible to change an element of an ObservableCollection directly by code, and the display is automatically refreshed?
Is it possible to somehow catch the SelectionChanged event before the handler is executed? Or is it possible to temporary dissable the event?
Any other idear?
Thank you for your help!
CollectionChanged does notify, that collection itself, not the
single items, is changed. Therefore to see the changes item's
property need to implement INotifyPropertyChanged. Also remove Mode=OneTime
You can of course set the flag, that PWMTyp_SelectionChanged is
running:
private bool selChangedIsRunning = false;
private void PWMTyp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(selChangedIsRunning) return;
selChangedIsRunning = true;
// do stuff ....
selChangedIsRunning = false;
}
Other idea is - don't use the SelectionChange event, but do bind
Slider.Visibility and TextBox.Visibility to the
ComboBox.SelectedValue and use value converter to define the
Visibilty, also you can use the ConverterParameter.
<ComboBox x:Name="CmbPWMTyp" ItemsSource="{Binding Path=Gebertyp, Converter={local1:EnumToCollectionConverter}, Mode=OneTime}"
SelectedValuePath="Value"
DisplayMemberPath="Description"
SelectionChanged="PWMTyp_SelectionChanged"
SelectedValue="{Binding Path=Gebertyp}" />
<TextBox Visibility="{Binding ElementName=CmbPWMTyp, Path=SelectedValue, Converter={StaticResource YourConverter}, ConverterParameter=TBX}" Text="{Binding GeberNmr, Mode=TwoWay}"/>
<Slider Visibility="{Binding ElementName=CmbPWMTyp, Path=SelectedValue, Converter={StaticResource YourConverter}, ConverterParameter=SLDR}" Value="{Binding Wert, Mode=TwoWay}"/>
This link can be also very helpful for you: Difference between SelectedItem SelectedValue and SelectedValuePath

How to implement the address bar we see in Google maps and other apps using Xamarin

Hello I'm new to Xamarin development. I am trying to implement the address bar feature in the application. When user starts typing the address I want to show the suggestions using Googles places API.
I have already got the background service calls to google and getting the suggestions in a LIST and trying to bind the list in the VIEW. But when binding the application is crashing and look of List is also doesn't look as expected.
View:
<Entry Placeholder="From Address" x:Name="FromAddressTxtbx" Text="{Binding AddressText}" TextChanged="Handle_TextChanged_1"/>
<ListView ItemsSource="{Binding Addresses}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Address}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Can anyone please provide me a suggestion or Direction I want this look like address bar in the UBER app.
Thank in advance
Here is a solution about using Entry and ListView to show updated list when text changed in Entry.
Xaml:
<Entry x:Name="MainEntry" TextChanged="MainEntry_TextChanged" Placeholder="Inupt Adress"/>
<ListView x:Name="NameListView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding .}" TextColor="Navy" FontSize="40"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ContentPage:First setting example data for ListView,
List<string> adressnames = new List<string>()
{
"Alabama", "Florida" , "Kentucky" , "Missouri", "North Carolina" ,"South Dakota" , "Wisconsin"
};
NameListView.ItemsSource = adressnames;
Now when Text changed , ListView will list updated data:
private void MainEntry_TextChanged(object sender, TextChangedEventArgs e)
{
var keyword = MainEntry.Text;
NameListView.ItemsSource = adressnames.Where(name => name.Contains(keyword));
}
By the way , you also can use SearchBar to achieve it:
<SearchBar x:Name="MainSearchBar" TextChanged="MainSearchBar_TextChanged" Placeholder="Inupt Adress"/>
With text Changed method:
private void MainSearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
var keyword = MainSearchBar.Text;
NameListView.ItemsSource = adressnames.Where(name => name.Contains(keyword));
}

How to Populate a ListPicker in Windows Phone 8.1 Silverlight without issues

this is my ListPicker on XAML :
<toolkit:ListPicker x:Name="CursoLista" Header="Curso" ItemsSource="{Binding}">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ListPickerItem Content="{Binding Curso}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
And Cs File :
ObservableCollection<FgCurso> ObjCurso = new ObservableCollection<FgCurso>();
The FgCurso Class
class FgCurso
{
public String Curso { get; set; }
}
And the instanciate of class
public void ListaCurso()
{
ObjCurso.Add(new FgCurso { Curso = "AdministraĆ§Ć£o" });
ObjCurso.Add(new FgCurso { Curso = "CCO" });
}
CursoLista.ItemsSource = ObjCurso;
And works fine, the problem is, when i put more than 5 itens inside of ListPicker it doesn't work,i got ,
I don't know how to solve that issue!
When a ListPicker has more than 5 items, it will open in full screen mode. The FullModeItemTemplate also should be provided for the items to be displayed in correct format while in full mode.
<DataTemplate x:Key="FullModeTemplate">
<.../>
</DataTemplate>
<DataTemplate x:Key="NormalTemplate">
<.../>
</DataTemplate>
<ListPicker ItemTemplate="{StaticResource NormalTemplate}" FullModeItemTemplate="{StaticResource FullModeTemplate}" />
The ItemTemplate will be used when the ListPicker expands in the page and the FullModeItemTemplate will be used when it opens in full mode.

WPF: ListView SelectedItem binding works for a split second, then changes back

let me start by introducing my current setup:
I have a ListView that binds its SelectedItem property to the ViewModel, like this:
<ListView Name="FileListView" ItemsSource="{Binding ImageList}"
SelectionChanged="ImageSelectionChanged"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<view:FileListItem />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
It's item template (view:FileListItem) is the following:
<Grid MouseDown="FileListItemMouseDown" KeyDown="FileListItemKeyDown">
....
<TextBlock Name="NewNameTextBlock"
Text="{Binding NewName}"
Grid.Column="2"
Visibility="{Binding TextBlockVisibility}" />
<TextBox Name="NewNameTextBox"
Text="{Binding NewName, UpdateSourceTrigger=PropertyChanged}"
Grid.Column="2"
Visibility="{Binding TextBoxVisibility}" />
</Grid>
The idea here is to switch on the TextBox and switch off the TextBlock when the corresponding ListView item is being edited. This works ok, but when I hit a particular key, I want the ListView to select the next item and put that item into editing mode. I catch the KeyDown event as seen above in the ItemTemplate and broadcast a message, which is caught in the DataContext of my ListView like this:
public ImageFile SelectedItem {
get { return _selectedItem; }
set { _selectedItem = value; NotifyPropertyChanged("SelectedItem"); }
}
public void SelectAndEditThisHandler (object x)
{
ImageFile file = x as ImageFile;
SelectedItem = file;
}
The result is that the selection actually changes for a split second, but then it changes back to the previous selection. I suspect some other UI elements might be handling my key-presses and doing something to change the selection back, but I can't figure out which elements and how to pinpoint them.
Any help would be greatly appreciated! Thanks!
EDIT:
As requested, the SelectionChanged handler:
private void ImageSelectionChanged(object sender, SelectionChangedEventArgs e)
{
System.Collections.IList filelist = FileListView.SelectedItems;
if (filelist.Count == 1)
{
ImageFile selectedFile = FileListView.SelectedItem as ImageFile;
Mediator.Instance.NotifyColleagues(Mediator.Operations.ImagePathSelected, selectedFile.OriginalPath);
}
}
The mediator message broadcast doesn't do anything related to these controls/this problem at all.

Get selected item from a ListBox at hold

I've got a ListBox in a WP7 App where I want to do something with an item when the user hold it. The event work's great. My hold method gets called, but I can't detect which element in the list was hold.
ListBox.SelectedItem is always -1 and a code from another post on stackoverflow doens't work:
FrameWorkelement element = (FrameworkElement) e.OriginalSource;
ItemViewModel item = (ItemViewModel) element.DataContext;
I get an InvalidCastException when running it in the second line.
The following code should work.
private void StackPanel_Hold(object sender, GestureEventArgs e)
{
ItemViewModel itemViewModel = (sender as StackPanel).DataContext as ItemViewModel;
string t = itemViewModel.LineOne;
}
Note: before using the DataContext of the sender object, make sure you cast the sender object to the correct class. In this example I use a StackPanel in my DataTemplate:
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Height="78" Hold="StackPanel_Hold">
<TextBlock Text="{Binding LineOne}" />
<TextBlock Text="{Binding LineTwo}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Categories