LongListSelector - How to MVVM bind the SelectedItem - windows-phone-7

I've managed to get the LongListSelector running through MVVM.
In other words the ItemSource is set through a property on my viewmodel.
But for some weird reason, I can't seem to be able to 'bind' the SelectedItem of the LongListSelector... I'm not getting in the Set nor Get of the ViewModel property.
How is this done? And what should the 'type' of the SelectedItem on the ViewModel be? I thought the Type of the Class inside the Group?
My current xaml:
<silverlighttoolkit:LongListSelector x:Name="AlbumsList"
Background="Transparent"
ItemTemplate="{StaticResource ItemTemplate}"
GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}"
GroupItemTemplate="{StaticResource GroupItemTemplate}"
ItemsSource="{Binding GroupedAlbums}"
SelectedItem="{Binding SelectedAlbum, Mode=TwoWay}">
<silverlighttoolkit:LongListSelector.GroupItemsPanel>
<ItemsPanelTemplate>
<silverlighttoolkit:WrapPanel />
</ItemsPanelTemplate>
</silverlighttoolkit:LongListSelector.GroupItemsPanel>
</silverlighttoolkit:LongListSelector>

Use the SelectionChanged event. Either though a EventToCommand behaviour, or a attached behaviour.

Related

Bind data inside ListBox using code-behind in ReactiveUI

I have WPF listbox:
<ListBox Name="FileDownloads" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="Url" Text="{Binding Url}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I like the ability to bind ListBox by name from code behind using:
this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource); Having binding in code-behind helps with refactoring.
Is there any way to bind Url texbox inside the listbox using code-behind?
Is there any way to bind Url textbox inside the listbox using code-behind?
Not at the moment. You can either use XAML bindings like you're doing now, or you can put your data templates inside UserControls.
A consolation to this somewhat more cumbersome approach, is that if you register your data template UserControls and implement IViewFor<TViewModel> on them:
Splat.Locator.CurrentMutable.Register(typeof(MyView), typeof(IViewFor<MyViewModel>));
Then, you can write the ListBox simply as:
<ListBox Name="FileDownloads" SelectionMode="Extended" />
This line will automatically wire up a DataTemplate for you:
this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);

Windows phone: How to select all checkboxes (populated by DATA BINDING ) on button click?

I have populated a listbox with checkboxes content using this code:
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="12,105,6,100">
<ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="393" Margin="0,0,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="xxx" Content="{Binding Path=DisplayName, Mode=TwoWay}" Unchecked="xxx_Unchecked" Checked="xxx_Checked"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Now I want to check all checkboxes on a button click, please help.
There are 2 options available to you.
Add another (Boolean) property to the object in the collection you are binding to the ItemsSource of the ListBox. You would then bind this to the IsChecked property of the checkbox. Once done, in response to your button click you would just need to iterate over the collection and set all the properties to true, then as long as you are notifying of property changed on the bool the UI will be updated.
You could walk the visual tree of the ListBox looking for checkboxes and checking all that you find.
My descriptions may make option 1 seem like more work but that's what I'd recommend.

Caliburn micro and list picker control

I am trying to use the list picker control for wp 7 and caliburn micro. I get the binding correct from the model with conventions, but when I press the the picker to see the page to select somethin gelse I get the message
PID:0E2108CA TID:0F790ABE 2012-04-30 18:02:20.7180 View Model not found. Searched: Microsoft.Phone.Controls, Microsoft.Phone.Controls.ListPickerPageViewModel.
PID:0E2108CA TID:0F790ABE 2012-04-30 18:02:20.7210 View Model not found. Searched: Microsoft.Phone.Controls.IListPickerPageViewModel, Microsoft.Phone.Controls.ListPickerPageViewModel.
and it loads a complete blank page (think its the ListPickerPage in the control toolkit)
it doesnt matter if its bound or not, i guess its some convention hooking in that I dont want.
To reproduce start a new project, hoock up a viewmodel and view, enter below in in your xaml
my xaml looks like this
<toolkit:ListPicker Header="Background" ExpansionMode="FullscreenOnly">
<sys:String>dark</sys:String>
<sys:String>light</sys:String>
<sys:String>dazzle</sys:String>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding}"
Margin="0 0 0 0"
FontSize="43"
FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
What I would like to happen is ofcourse that the property of my model should popuplate the fullscreen selection, and also it would be nice to set the initial selection based on a property on the model.
Some points I noted:
You have to provide an ItemsSource to the ListPicker - A collection of some sort from which it can display the items. In TextBlock text={Binding } - You have to bind some property, so that it can display.

How to set a visibility of TextBlock bound to a ListBox?

How can I set a TextBlock visibility inside a ListBox. For example
<Grid>
<ListBox Name="myList">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBloxk Name="txtBlock1" Text="{Binding} First Name">
<TextBloxk Name="txtBlock2" Text="{Binding} Last Name">
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
At run time, I want to set the visibility of either textblocks to either Visible or Collapsed.
How can I do that, or can I?
You can use BooleanToVisibilityConverter (or other, custom converter) to change the Visibility according to binding value.
Use VisualTreeHelper if you need to iterate over each of the items in the ListBox. Or, re-bind ListBox to the new set of data and use MarcinJuraszek's suggestion on using a Converter (definitely an easier choice :))

Using a ListBox control in a DataTemplate

I have a simple WP7 Programm where I want to switch between displaying my model objects in a ListBox and a Diagramm.
I want to use Data Templates and a Selector Class which returns the correct template.
The selector takes a boolean property in the view model and returns ListBoxTemplate or DiagrammTemplate
My Page Resources looks like this:
<local:NewTemplateSelector x:Key="NewTemplateSelector">
<local:NewTemplateSelector.ListBoxTemplate>
<DataTemplate>
<StackPanel>
<ListBox
x:Name="MainListBox" Margin="6,205,35,136" ItemsSource="{Binding Acts}"
ItemTemplate="{Binding ElementName=Page, Path=Orientation,
Converter={StaticResource OrientationToListItemTemplate}}" />
</StackPanel>
</DataTemplate>
</local:NewTemplateSelector.ListBoxTemplate>
<local:NewTemplateSelector.DiagrammTemplate>
<DataTemplate>
<TextBlock Text="Diagramm"/>
</DataTemplate>
</local:NewTemplateSelector.DiagrammTemplate>
</local:NewTemplateSelector>
My content Panel has only 1 element:
<ContentControl ContentTemplate="{Binding IsDiagramm,
Converter={StaticResource NewTemplateSelector}}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" />
I always get a blank screen when I run this.
My Selector class returns the correct template, I can see this in the debugger.
When I replace the Listbox in the template with a simple textblock, the textblock is displayed, so I suspect a problem with databinding.
But the listbox in the template works fine, when I insert it in my content panel without any data templates.
Any hints for me?
You may like to refer to this thread which discusses nested listboxes.
Listbox Inside listbox Databinding Problem

Resources