Bind data inside ListBox using code-behind in ReactiveUI - 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);

Related

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.

How to make a custom listview with one image and one textbox in windows7 phone?

i need to make a custom listview in windows7 phone. In this listview, i need to show one image with one textbox. This listview will be dynamic. i will insert value in database and this listview will be generate till the last value of my database.
I am new in windows7 phone, any suggestion will be appricated.
You need to set the ItemTemplate:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=ImageSource}"/>
<TextBlock Text="{Binding Path=Text"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox>
The above code assumes you have bound a list, or set the ItemsSource, to a list of objects that expose Text and ImageSource properties.

LongListSelector - How to MVVM bind the SelectedItem

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.

Windows Phone 7 Linking to events inside DataTemplates

I have a listbox where I create a Itemtemplate with a DataTemplate. I want to be able to write events for the checkboxes and buttons in the datatemplate but they do not seem to be firing.
Here is my xaml and basically I just tried to display a messagebox.show("worked") in the event function.
<ListBox x:Name="ListBox_Items" Margin="0,91,0,8" Foreground="#FF4BE5DB">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="700">
<CheckBox IsChecked="{Binding needPurchase}" Click="NeedPurchase_Click" Name="CheckBox_NeedPurchase"/>
<CheckBox IsChecked="False" Name="InCart"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding storeLocation}"/>
</StackPanel>
<Button HorizontalAlignment="Right" Content="DELETE" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Because the items are defined within a DataTemplate they are not hooked up to the code-behind for the parent class. If you want to handle events for templated items, then you should consider using commands instead. If you don't know what commands are (and therefore unlikely to know what MVVM is), then you should check out an explanation like this by Jeremy Likness.
I agree that using commands is the best approach.
However if you still want to assess controls placed inside the ItemTemplate/DataTemplate (and subscribe to some events), then you can do this by using the VisualTreeHelper.
For starters you need to remove the name from all the controls in the template. If you have 10 items in the list you will have 10 sets of controls with the same name which won't work.

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