How to use Pivot Effectively? - c#

I have following data structure in my WP7 app. And I'm generating three PivotItems through databinding, contents of binding. Interesting part is when Binding happens for Pivot items contents(Items) are queried for three time and again selection changes.
Is there anything I'm doing wrong?
Code:
<controls:Pivot Title="{StaticResource ApplicationName}" ItemsSource="{Binding Folders}" SelectedItem="{Binding SelectedFolder, Mode=TwoWay}" Name="_pivot">
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox DataContext="{Binding Source={StaticResource Locator}}" ItemsSource="{Binding ThingsListViewModel.Items}" />
</DataTemplate>
</controls:Pivot.ItemTemplate>
I have three folders items, when Pivot control is created ThingsListViewModel.Items property executed thrice, and once every time selection changes.
I'm expecting ThingsListViewModel.Items to execute only selection chage on Pivot control.

I think you need to listen to the LoadedPivotItem and Loaded events of the pivot. The Loaded event will always load the first PivotItem (LoadedPivotItem) will not be called. The LoadedPivotItem is called when the user swipes to another PivotItem.
Based on these events you should then run your queries for the currently SelectedPage. You may also want a flag to indicate once you have loaded the data for each Pivot to avoid running the queries again.

Related

WPF ListView handle single and double click differently

I have a list view with some items, and a Style that specifies that when an item is clicked, the IsSelected property will change. See an excerpt of the code below.
Currently this selects/highlights an item and performs an action related to that item when clicked on in the list. Now I want to change the behaviour so that this happens on double click instead of single click. In addition, when an item is single clicked, that item should also be selected/highlighted, but the action related to that item is not performed, so that the user can select several items by single clicking them (similar to how you would do ctrl-click usually) and then performing all actions at once when the desired items are selected and some button clicked.
I should also mention that I am using the MVVM pattern, so there is no code in the code-behind file. Any code would go in the ItemListViewModel.cs file.
I suspect I would have to use Command in some way? I also found an attribute for ListView called MouseDoubleClick="", however this would not replace the single click functionality, only come in addition to it.
Does anyone have an idea how to make this work?
<ListView Grid.Row="0" x:Name="ItemsList"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True"
ItemsSource="{Binding FilteredItems}"
SelectionMode="Extended">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}" >
<Setter Property="IsSelected" Value="{Binding Selected, Mode=TwoWay}"/>
</Style>
</ListView.Resources>
</ListView>
For the selection behavior, take a look at SelectionMode. You can set ListView.SelectionMode to Multiple, which sounds like the behavior you're describing.
For the double-click, you can use the MouseDoubleClick event, or you can try using a MouseBinding which lets you use ICommands instead of events (the page on InputBinding has some good examples).
You'll have to test to see how everything plays together, though. Handling both single and double click can be tricky, and you might be forced to handle the PreviewMouseDown event and do everything manually (worst case scenario).

CaliburnMicro + RowDetailsTemplate: Details-View disappears

I have a GridView which lists all my Products. The first column is a "GridViewToggleRowDetailsColumn" which expands a Details-View when clicked.
I use CaliburnMicro to load the Details-View like so:
<telerik:RadGridView ItemsSource="{Binding Products.View}"
SelectedItem="{Binding SelectedProduct,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
EnableRowVirtualization="False"
ShowGroupPanel="False"
RowDetailsVisibilityMode="Collapsed"
RowIndicatorVisibility="Collapsed"
cal:Message.Attach="[Event RowDetailsVisibilityChanged] =
[Action OnRowDetailsVisibilityChanged($eventArgs)]">
<telerik:RadGridView.RowDetailsTemplate>
<DataTemplate>
<ContentControl cal:View.Model="{Binding DataContext.ProductDetailsViewModel, RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadGridView}}" />
</DataTemplate>
</telerik:RadGridView.RowDetailsTemplate>
<telerik:GridViewToggleRowDetailsColumn />
...Columndefinitions...
</telerik:RadGridView>
I can open and close the details as long as I stay on the same product. The problem occurs if I open the details while another details is already visible. Then the details of the first disappears and never comes back. See the screenshot:
The grey box is a placeholder for the details. I opened the details of the first 8 items one after another, but only the last one is showing. My guess is that Caliburn Micro can not locate the View anymore, but I have no idea why? Because I only have one ProductDetailsViewModel?
Also: in debug mode i noticed that the "ProductDetailsViewModel"-getter is accessed only once per item after expanding the row. Collapsing and expanding the same row does not access the getter again.
UPDATE:
Another approach, same effect: I say my ItemsSource is a collection of ProductDetailsViewModel and inherit my MainViewModel from Conductor<ProductDetailsViewModel>.Collection.OneActive, make the Grid binding with
ItemsSource = "{Binding Items}"
and in the xaml
<ContentControl cal:View.Model="{Binding DataContext.ActiveItem, RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadGridView}}" />
in the SelectionChanged I do
ActivateItem(e.Row.Item as ProductDetailsViewModel);
Same as before, the ProductDetailsView is displayed for the current item, but disappears once I show the details of another product.
The problem was the view-caching of "IViewAware", which is automatically implemented by screens. Overwriting the "GetView" in the ProductDetailsViewModel solved the issue.
Related: Screens shared in multiple screens disappear after deactivation in Caliburn.Micro

Telerik DragDrop Does not work

I've been developing a software and i want to use telerik Listbox drag&drag.
the problem is when i drop an item from one listbox to another it does not work at all.
It seems that the drop event does not fire.
I set allowdrop= true
I added dragvisualprovider
I set allowreorder = true
I also tried to write code when dragLeave and PreDrop Events Fire and add that item into the new Listbox. but it works sometimes correct by chance!!!
I don't use MVVM Model
based on what telerik site says, I don't use static items collection. I create new items in my page load and add it to first Listbox.
You have to use <telerik:ListBoxDragDropBehavior AllowReorder="True" /> like this:
<telerik:RadListBox ScrollViewer.VerticalScrollBarVisibility="Visible" SelectedItem="{Binding DataContext.SelectedItem, ElementName=editor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource DraggableListBoxItem}" DisplayMemberPath="DisplayName" ItemsSource="{Binding LeaveActions,Mode=TwoWay}" Grid.Row="3" AllowDrop="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<telerik:RadListBox.DragDropBehavior>
<telerik:ListBoxDragDropBehavior AllowReorder="True" />
</telerik:RadListBox.DragDropBehavior>
</telerik:RadListBox>
Than every thing should work well. If you want to get into the drag/drop events, just use the telerik DragDropmanager.

Manipulation many object by one

I have situation
<Grid>
<FlipView x:Name="flip1">
...
</FlipView>
<FlipView x:Name="flip2">
...
</FlipView>
</Grid>
How I can synchronize manipulations on these two FlipView controls?
If user makes 'swipe' gest on grid, booth of FlipViews should change page.
You could try binding SelectedItem property of these two - e.g. set SelectedItem={Binding SelectedItem, ElementName=flip2, Mode=TwoWay} on flip1. The results with touch manipulation feedback might not be ideal though and you can't make it perfect even if you try to manually synchronize the offset on the ScrollViewer in the template of each of the FlipViews.

Select an item from checkedlistbox using wpf,mvvm

I am new to MVVM, I have a checkedlistbox in a view with the list of titles(have bound the exposed property in ViewModel to this checkedlistbox control)...
Here is my XAML code that populates the ListCheckBox -
<ListBox x:Name="lstCode" ItemsSource="{Binding Code,Mode=TwoWay}" Grid.Row="1" Style="{StaticResource ListBoxStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chkBox" IsChecked="{Binding IsChecked,Mode=TwoWay}" Content="{Binding Code_Name}" Margin="0" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This control shows the correct list of items with checkboxes for each item in the listbox...
What should be the code in viewmodel to make it work in two way - while getting the codes from database, it should automatically selected the code from the listcheckedbox and when the user selects one or more codes, the viewmodel should be able to know the items selected...
In general, for TwoWay binding, you will need to implement the INotifyPropertyChanged interface on the ViewModel you want to bind to.
In this case, your ViewModel will have to provide a property that returns a collection that your view can bind to, e.g. an ObservableCollection.
This ObservableCollection already allows you to add, update, and delete items in that list in a way that automatically communicates the changes between View and ViewModel.
For the rest I suggest to start digging into MVVM depths. To fully take advantage of WPF's capabilities, you will need to understand the basics for yourself. A great starting point is this SO thread: MVVM: Tutorial from start to finish?

Categories