SelectedItem set to first item with CollectionViewSource - windows-phone-7

I have a view databound through mvvm light to a viewmodel in my WP7 project.
The view contains a Listbox with following settings:
<ListBox x:Name="StationList"
ItemsSource="{Binding StationList}"
SelectedItem="{Binding SelectedStation, Mode=TwoWay}"
>
The StationList is a ObservableCollection.
Now when the view gets loaded, everything looks great! The list is shown and NO item is selected!
But when I change the XAML to:
<ListBox x:Name="StationList"
ItemsSource="{Binding Source={StaticResource StationListSorted}}"
SelectedItem="{Binding SelectedStation, Mode=TwoWay}"
>
With the StationListSorted being a simple one property sort on the StationList as a CollectionViewSource.
Now things turn ugly!!
The same view is loaded with the same items in the listbox, but now correctly sorted, BUT the first item is selected and the selectedItem property is set!!
How can I sort a ListBox with a CollectionViewSource WITHOUT it auto selecting my first item?

On your listbox, try setting IsSynchronizedWithCurrentItem and see which value (either true or false) produces the desired effect.

Related

How can I set the BindingContext of each view in a CarouselView?

I'm trying to let each view in a CarouselView have the same BindingContext object as the parent ContentPage. I've tried the following which doesn't seem to work. MainPage.xaml is the page that is initialized at runtime. It holds a CarouselView with four ContentViews holding "pages" of functionality I can swipe back and forth between from the MainPage.
MainPage.xaml:
...
<CarouselView Grid.Row="1" Grid.ColumnSpan="4"
Position="{Binding CarouselPosition}"
HorizontalScrollBarVisibility="Never">
<CarouselView.ItemsSource>
<x:Array Type="{x:Type ContentView}">
<local:HomeView></local:HomeView>
<local:NewsView></local:NewsView>
<local:ChartsView></local:ChartsView>
<local:SettingsView></local:SettingsView>
</x:Array>
</CarouselView.ItemsSource>
<CarouselView.ItemTemplate>
<DataTemplate>
<ContentView Content="{Binding .}" BindingContext="{Binding BindingContext, Source={x:Reference mainPage}}"/>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
...
Basically, I want to be able to set the BindingContext for each of these ContentViews to be the same as the parent's. I've also read that the ContentViews should be able to inherit the BindingContext sometimes, but it doesn't seem to be happening in this situation.
Thanks.
This is because this kind of control doesn't inherit its BindingContext from its parent but it has its own BindingContext set to the item of the collection binded to the ItemsSource which makes sense.
Let's say you want to display a collection of User. The DataTemplate will be the user interface of a User. You want to display some properties of a User, not your ViewModel.
If you want to bind to something exposed in the ViewModel and not the User, you have to bind the BindingContext of your element with either a RelativeSource or by targeting an element by its x:Name.
Exemple : to bind a button inside your DataTemplate to a command exposed in your ViewModel (not in User), you need to do that :
<Button
Text="Try me"
Command="{Binding
Path=BindingContext.MyViewModelCommand,
Source={x:Reference xNameOfParentOutsideDataTemplateOrThePage}" />
To make it easier to understand, what it does is : I want to bind to something on the control called xNameOfParentOutsideDataTemplateOrThePage (x:Name="xNameOfParentOutsideDataTemplateOrThePage"). On this control, I want to a property of its BindingContext (most of the time, your view model).
Instead of x:Reference, you can also use a RelativeSource binding.

How to use set automatically row height on Xamarin Forms?

I'm trying to make a ListView which has a DataTemplateSelector on ItemTemplate. Basically, it looks like an expandable cell, which shows more info when the item is selected.
My code on the DataTemplateSelector:
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return ((ItemAjuda)item).Selecionado ? ExpandedTemplate : SimpleTemplate;
}
and this is my code on the view:
<ListView
x:Name = "listView"
SeparatorVisibility = "None"
RowHeight = "-1"
HasUnevenRows="true"
ItemsSource="{Binding Itens}"
SelectedItem="{Binding ItemSelecionado, Mode=TwoWay}"
BackgroundColor = "{StaticResource DefaultBackground}">
<ListView.ItemTemplate>
<local:AjudaTemplateSelector/>
</ListView.ItemTemplate>
</ListView>
It works perfectly on Android, but on IOS the cell got a strange behavior. The row height changes but it doesn't looks like an uneven row :(
Ps: I've already tried to ForceUpdateSize() on the cell, but unfortunately it doesn't change anything (maybe because of the DataTemplateSelector?)

how to checkbox checked for all in listbox datasource in wp8?

while selecting all means renaming all the checkbox are checked. my code is below:
<ListBox Name="VillageList">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding isCheched}" Content="{Binding Villages}" IsChecked="False"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
so all checkbox are inside the listbox.how to checked is enable for all checkbox single selection.
public class villageinformation
{
public string Villages{get;set;}
public bool isChecked {get;set;}
}
page.cs{
list<villageinformation> mydataSource=new list<villageinformation>();
myDataSource.Add(new villageinformation(){Village="All",isChecked ="False"});
myDataSource.Add(new villageinformation(){Village="name1",isChecked ="False"});
myDataSource.Add(new villageinformation(){Village="name2",isChecked ="False"});
myDataSource.Add(new villageinformation(){Village="name3",isChecked ="False"});
VillageList.itemSource=myDataSource;
}
so while clicking manually "All" checkbox remaining name1,2,3 are selected how to do this?
If you are using the ObservableCollection for your villages list, which is used for the data binding to the ListBox, then you can set all values in the ObservalbeCollection in the codebehind which shoul update the UI (by checking all the Checkboxes)
you can do it something like this
foreach(var village in VillagesCollection)
{
village.isChecked = true;
}
If you are not able to understand the above, edit your question with how you are setting up the Village data in the code behind file (means .xaml.cs file)

How to print all the values using linq?

I have got a listbox and I have to populate that with the elements from the query. The query is,
var query = from b in context.table select b;
List<Tab> reclist = q.ToList();
using LINQ how can I print all the obtained values in the listbox?
You'll need to create a DataTemplate containing a TextBlock whose Text property you bind to a property on your object. So, some XAML something like;
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Presuming your Tab class has a property called Name. Obviously if it doesn't you'll want to change the {Binding Name} portion of the XAML to match the property name that you want to display in the ListBox.
You then bind to your item;
MyListBox.ItemsSource = reclist;

Panorama Title binding

I'm doing WP7 app using Panorama control and have a problem with binding into Panorama Title property. Is it possible to bind that value out from ViewModel object?
Binding in xaml file:
<controls:Panorama x:Name="prmPanorama" Title="{Binding Voyage.Title}">
Voyage property of ViewModel is a Model entity (with Title property inside) with OnNotifyPropertyChanged event fired every time it changes:
private Voyage _voyage;
public Voyage Voyage
{
get { return _voyage; }
set
{
if (_voyage != value)
{
_voyage = value;
OnNotifyPropertyChanged("Voyage");
}
}
}
When I bind the same property into another control, eg. TextBlock, binding works just fine:
<TextBlock Text="{Binding Voyage.Title}" />
The text shown in that text block is as it should be but on the same time panorama title is not binded right - it's collapsed.
Does anyone tried to do that kind of binding? I have no idea why it doesn't work.
<DataTemplate x:Key="TitleDataTemplate">
<TextBlock Text="{Binding}" />
</DataTemplate>
...
<controls:Panorama Title="{Binding Voyage.Title}"
TitleTemplate="{StaticResource TitleDataTemplate}">
The control template of the panorama control uses a content presenter to display whatever value the its title property has kind of like a button. When setting the title template property, you indirectly set the content template of the content presenter.
That is why you have to set the title property on the panorama control and then can use that value in your title template for binding. In other words its not enough to just bind to the title you have to give it a template.
Check out this link for more info

Resources