Databinding with Data Template on ComboBox Select Item using LINQ - linq

I have a little Problem.
A SP give me a several list of URL´s. The Urls are binded on a Combobox.
When I Select an Item, always the Object comes to the Combobox not the Selected Value, see the code below:
<DataTemplate x:Key="Webadressen" DataType="{x:Type src2:GetWebadressenResult}" >
<StackPanel>
<Label Content="{Binding Path=Adresse}" />
</StackPanel>
</DataTemplate>
<ComboBox Width="192" IsEditable="True" Margin="2" Name="Cbox_GDWeb" ItemTemplate="{StaticResource Webadressen}" SelectionChanged="Cbox_GDWeb_SelectionChanged">
private void Cbox_GDWeb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
GetWebadressenResult test = (GetWebadressenResult)this.Cbox_GDWeb.SelectedValue;
MessageBox.Show(test.Adresse.ToString());
this.Cbox_GDWeb.Text = test.Adresse.ToString(); /* Not Working cause the this Event calls the same Method */
}

Change your ComboBox to this
<ComboBox Width="192" IsEditable="True" Margin="2" Name="Cbox_GDWeb" DisplayMemberPath="Adresse" SelectedValuePath="Adresse" SelectedValue="{Binding Path=Adresse}">
then you wont need the Datatemplate and it will work fine
you also wont need the SelectionChange event to change your selecteditem

Related

Navigate to specific page defined on model when Item is Tapped

I'm trying to make a custom home page where pages are listed on an Horizontal scroll view as "Services" so each one of them should navigate to a different Page.
I have a view like this:
<controls:HorizontalScrollView HeightRequest="160"
Orientation="Horizontal"
ItemsSource="{Binding OwnerServicesList}"
x:Name="OwnerServicesSlider"
ItemSelected="OwnerServicesSlider_ItemSelected">
<controls:HorizontalScrollView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Margin="10,0,5,0" WidthRequest="100" HeightRequest="100">
<Image HorizontalOptions="Start" Source="{Binding ImgUrl}" WidthRequest="100" HeightRequest="100" />
<Label Style="{StaticResource BoldLabel}" HorizontalTextAlignment="Center" FontSize="13" LineBreakMode="TailTruncation" Text="{Binding Name}" TextColor="Black"/>
</StackLayout>
</ViewCell>
</DataTemplate>
Im using a custom made controller for a Horizontal Scroll view that works like a listview, every item on tap raises a ItemTappedEventArgs event.
Inside my model i´ve declared a public Page Page { get; set; } for each object in the scroll view.
What im trying to do is recover the tapped element and recover the Page stored in it so that I can Navigate to that specific page.
So far I have something like this:
private void OwnerServicesSlider_ItemSelected(object sender, ItemTappedEventArgs e)
{
var service = OwnerServicesSlider.SelectedItem as Services;
Navigation.PushAsync(service.Page);
}
It shows no errors but when I run it I get a
InvalidOperationException: 'Page must not already have a parent.
Any hint will be appreciated!
as Jason said,maybe the page you would push which is exist in thecurren navigation structure,there is a workaround ,before you push the page :
private void OwnerServicesSlider_ItemSelected(object sender, ItemTappedEventArgs e)
{
var service = OwnerServicesSlider.SelectedItem as Services;
service.Page.Parent = null;
Navigation.PushAsync(service.Page);
}

Is there a way I can pass a grid name or a parameter into a tapped event?

I have this code and somehow I want to get the text value of the first label to the tapped event in the CS code. As the tapped event is on the grid I had an idea of putting the text value into x:Name.
<ViewCell >
<Grid VerticalOptions="CenterAndExpand" x:Name="{Binding [0].Name}" Tapped="atiSelectValueX" >
<local:StyledLabel Text="{Binding [0].Name}" HorizontalOptions="StartAndExpand" />
<local:StyledLabel IsVisible="{Binding [0].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="x" />
</Grid>
</ViewCell>
The CS code I have so far:
void atiSelectValueX(object sender, EventArgs e)
{
var cell = sender as Grid;
if (cell == null)
return;
var selected = cell. <<< I want to get the name here
What I would like to do is to get the x:Name value in the CS code. I was hoping to get the sender information but it seems like I cannot enter
cell.Name
Is there another way I can get a parameter like the name (which is the same as the text in the first label always) in the C# code?
The x:Name is metadata used by the XAML tools, not an actual property of the object. You could also bind Name to an unused property, like StyleID, and access that instead
<Grid VerticalOptions="CenterAndExpand" StyleId="{Binding [0].Name}" ...
and then
var selected = cell.StyleId;

Get selecteditem from listbox using MVVM

I'm using MVVM in this project, I have a listbox which bind to a collection of Customers. I want to create an event to navigate a detailsPage using id of the elementselected:
<ListBox ItemsSource="{Binding Customers}" x:Name="state_list" SelectionChanged="state_list_SelectionChanged">
<i:Interaction.Triggers>
<i:EventTrigger EventName="selectionchanged">
<cmd:EventToCommand Command="{Binding stateSelectedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding nom}" />
<!--TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Text, ElementName=tbCount}" /-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I can't figure out how to get the selected item to add it to the uri and then use it to get data. An example or tutorial would be helpful. Thanks :)
I would create a "SelectedCustomer" property in the ViewModel (next to you Customers property) and bind it to the SelectedItem. Then, on the setter of that property you can navigate to your desired page. This way you eliminate the messy events and command.
<ListBox x:Name="state_list
ItemsSource="{Binding Customers}"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}">
...
public Customer SelectedCustomer
{
get
{
return _selectedCustomer;
}
set
{
if (value != null)
{
_selectedCustomer = value;
//Navigate to your page here, either with Navigator class or any other mechanism you have in place for changing content on screen
}
}
}
AlexDrenea gives you a good way of binding SelectedItem to a property on your viewmodel. If you are wanting to navigate based on this in an MVVM architecture, I would suggest using messaging to get it done.
I cover this in a blog post I did a while back, but the short summary of doing this within MVVMLight, is to create a Navigator class that sits at the application level.
public class Navigator
{
private PhoneApplicatoinFrame RootFrame;
public Navigator(PhoneApplicationFrame frame)
{
RootFrame = frame;
RegisterMessages();
}
private void RegisterMessages()
{
Messenger.Default.Register<ShowTrackerMessage>(this, ShowTracker);
}
private void ShowTracker(ShowTrackerMessage msg)
{
RootFrame.Navigate(new Uri("/Views/ItemLocationCompassView.xaml", UriKind.RelativeOrAbsolute));
}
}
Then, as part of your application start-up, create it and pass it a reference to your RootFrame:
private static Navigator _navigator;
public static Navigator Nav
{
get { return _navigator; }
}
...
_navigator = new Navigator(this.RootFrame);
Then, you have a couple choices on how you send the Navigation message.
Option 1: In your ViewModel, hook into the PropertyChanged event (part of INotifyPropertyChanged), and send the appropriate message when your SelectedItem property changes.
Option 2: Tie into the SelectionChanged event of your ListBox. I use the MVVMLight's EventToCommand to send that event to a RelayCommand in my ViewModel, then react appropriately to send the message to the Navigator object.
I cover this in more detail at: http://www.smartchitecture.com/?p=27

WP7 - accessing selected item in listbox when listbox is within an itemtemplate

I have a Pivot item template that includes a listbox
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItemHeaders}" Title="CLASS TIMETABLE" >
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox x:Name="Events" ItemsSource="{Binding allEventItems}" ItemTemplate="{StaticResource EventDisplay2}"/>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
In the code behind I want to access the selectedItem of that listbox but I cannot 'get' to the listbox as such because ity is (presumably) within the template
i.e
this.NavigationService.Navigate(new Uri("/View/EventEdit.xaml?selectedEvent=" + Events.SelectedItem, UriKind.Relative));
The Events listbox is not being recognised.
Assuminh I can pass get the object and pass it through as a parameter, what code can I use to retrieve it
I know its starts with
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("SelectedEvent"))
{
But I am unsure of the syntax/code to extract out the object from the parameters
Appreciate how I can get the selectedItem from this listbox and the code to get the object being passed through
thanks
Rather than attempt to access the ListBox, you could use the SelectionChanged event to be told when the value changes:
<ListBox x:Name="Events"
ItemsSource="{Binding allEventItems}"
ItemTemplate="{StaticResource EventDisplay2}"
SelectionChanged="Event_SelectionChanged" />
And then in your code behind:
private void Event_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.selectedEvent = (EventItem)e.AddedItems[0];
}
You can access the value using NavigationContext.QueryString["selectedEvent"], but you can only store strings in navigation query strings. If your listbox is currently bound to objects, you'll need to select a key and then find that event from the second page using that key.

Passing Object rather than selectedIndex from Databound listbox

I'm having an issue when I try to pass the selectedIndex of my list from my "List" screen.
On my List screen I have the following binding:
Code Behind:
lbPrograms.ItemsSource = App.ViewModel.Items;
XAML:
<ListBox x:Name="lbPrograms" ItemsSource="{Binding Items}" SelectionChanged="lbPrograms_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<Image x:Name="ItemImage" Source="/images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding programName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="DetailsText" Text="{Binding createDate}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="programId" Text="{Binding programId}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
<!--<Image x:Name="ItemFavs" Source="/images/favs.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<Image x:Name="ItemDelete" Source="/images/delete.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In my detail screen I look up using the selectedIndex value:
App.ViewModel.Refresh();
DataContext = App.ViewModel.Items[index].nValDictionary;
However, since I can't figure out how to just update a value in my iEnumerable collection I have been removingAt[index] and then re-adding to the collection.
So can anyone tell me, how do use update an existing element in my collection? If not, can I pass the programId (that is in my binding) instead of the selectedIndex as the indexes are getting all messed up after the Delete/Add functionality.
Please Advise.
UPDATE:
After speaking in several forums I should clear up that I am implement INotifyChanged Event on my properties in my object.
Basically the following snippet of code is my current issue:
private void FavBtn_Click(object sender, EventArgs e)
{
foreach (var item in App.ViewModel.Items)
{
if ((item.currentProgram == true) && (item.programId != index))
{
item.currentProgram = false;
}
if (item.programId == index)
{
item.currentProgram = true;
}
}
When I run this everything looks to be ok.
However, when I navigate to another page, I re-load the object and the changes are lost. It is almost like I need to save them before navigating, however, if I do a item.Save(); I get duplicates in my list.
I'm guessing you haven't implemented INotifyPropertyChanged on your Items class yet.
Implementing this will allow updates made in these objects to be updated through your data bindings.
Here's a walkthrough on how to implement INotifyPropertyChanged.
How to: Implement the INotifyPropertyChanged Interface
Is App.ViewModel.Items of type ObservableCollection<T>?
If so, when you modify a property of one of the collection items and raise a property change event (via INotifyPropertyChanged) indicating the collection property that your ListBox.ItemsSource is bound to, the DataTemplate bindings will do the rest.

Resources