Navigation in Silverlight 5 - silverlight

I want to navigate to new page on the click event of Next button. Suppose my current page is "FirstTestPage.xaml" and want to go on the page "SecondTestPage.xaml" on click on nextButton_Click Event
private void RadButton_Click(object sender, RoutedEventArgs e)
{
///CODE HERE
}
And also want the back button which transfer me to FirstTestPage.xaml.
Thank You

Please see my answer to the question: Making a Wizard in Silverlight + Xaml
to quote it: "I would suggest looking into the Silverlight Navigation Framework. It allows you to use "urls" to navigate between "pages" (which are your XAML user controls). It also also users to use the back and forth buttons in the browser, which may or may not be something you want to allow.
There is a VS 2010 template when you choose New Project, Silverlight, "Silverlight Navigation Application" that will help get you started."
You can also use a border or panel in the Xaml and change the content of that. Here's an example using border.
XAML
<Grid>
<Border x:Name="contentBorder" />
</Grid>
Code Behind:
private void RadButton_Click(object sender, RoutedEventArgs e)
{
this.contentBorder.Child = new SecondTestPage();
}
If you want to get fancy and have the Telerik controls (which I assume from your RadButton example) check out their transition control: http://demos.telerik.com/silverlight/#TransitionControl/FirstLook

Related

WPF How to handle Cut, Copy, Paste toolbar buttons with UserControls and WindowsFormsHost

I'm hoping someone out there can help me. I'm trying to convert a legacy winforms app to WPF using MVVM. I've broken up the main window of the application into 4 main UserControls. The UserControls display different types of data objects and each UserControl has it's own ViewModel. Some of the data objects are inter-changeable between the different UserControls, for instance 'User Control 1' can contain strings objects and so can 'User Control 2 and 'User Control 3' (see diagram below).
My question is how can I handle the Cut, Copy, Paste commands in the toolbar? To possibly make things more complicated, each UserControl can contain a selected object at the same time as the other UserControls contain a selected object and User Control 2 is a WindowsFormsHost wrapper around a winforms control.
So far I've tried using ApplicationCommands but I can't even get them to fire. I've pasted a snippet of the code I thought would work using the ApplicationCommands below. Any help with this would be really appreciated.
<Button Command="ApplicationCommands.Cut" />
and on the UserControls
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.Cut" Executed="executed_Cut" CanExecute="canExecute_Cut" />
</UserControl.CommandBindings>
and finally in the UserControl's code behind (I know this isn't great)
public void executed_Cut(Object sender, ExecutedRoutedEventArgs e)
{
//execute code here
}
public void canExecute_Cut(Object sender, CanExecuteRoutedEventArgs e)
{
//can execute code here
}
I successfully use the behavior approach described in this question to avoid putting any code in my view model. Then whenever focus goes to a control which has a copy/paste behavior defined the toolbar cut/copy/paste buttons "light up" accordingly.

Hide navigation bar but remember the history in wpf frame

I have a frame on my main window. I have an ItemsControl in which I show the vertical menu from which user can select any Item and the page corresponding to specific Item is shown in the frame.
This works well.
When I set NavigationUIVisibility to Automatic:
1) Navigation Bar is displayed at the top.
2) If I enter some data in a textbox in page1 and then if I navigate away to page2.
Now again if I navigate to page1, the text in the textbox is there.
What I want is:
1) Hide the Navigation bar. For that I have set NavigationUIVisibility to Hidden.
This works good.
2) I want frame to remember history as I discussed in point 2 in above topic.
So, How to remember history when NavigationUIVisibility is set to hidden.
Or is there any other way to remember history when navigation bar is hidden.
You can re-style NavigationFrames and NavigationWindows so that they look completely different. In fact all of these images below are NavigationWindows, even the one without any navigation UI at all.
I put together an open source library for re-styling these at http://winchrome.codeplex.com so you can just steal the parts you need from the style. In fact if you just want a navigating panel with menu on the left, then its already covered in some of the demos for that project.
To answer your question as to why the TextBox clears it depends very much on what you are navigating to, and for some cases this is independent of the history.
Lets start simply, lets consider we have MyPage.xaml
<Page x:class="MyPage"...>
<TextBox/>
</Page>
If you are navigating to the same MyPage that you were before, then it will still have the same values in it.
If you are navigating to a new MyPage() then by default the TextBox will be empty.
If the navigation history is turned on, and you are navigating back, then you get the original MyPage and so see the same value in the TextBox.
Now lets consider if we are using MVVM style views bound to ViewModels.
In MyPage.xaml
<Page ...>
<TextBox Text="{Binding MyData}"/>
</Page>
In DataViewModel.cs
public class DataViewModel : INotifyPropertyChanged
{
private string myData;
public string MyData
{
get { return myData;}
set { ... }
//Normal implementation of INotifyPropertyChanged etc for MVVM
}
If you are navigating to a new Page() which is bound to the same ViewModel then both Pages will always be in sync and show the same data.
If navigating to the ViewModel and relying on DataTemplates to create a view, then you have still have two views but both sync from the same ViewModel.
If you use the history and navigate back, you get the original MyPage so see the original data again.
The Frame class has a property called BackStack which retains the back navigation history for a Frame. This property is available to you regardless of the NavigationUIVisibility setting.
Use PageFunction instead, which helps out you to navigate very backward retaining values for that pages..
Page1.xaml:
<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="XYZ"
x:TypeArguments="sys:String"
Title="page1">
<TextBox x:Name="textbox"/>
<Button x:Name="button" Click="button_clicked"/>
</PageFunction>
private void button_clicked(object sender, RoutedEventArgs e)
{
Page2 page2 = new Page2();
page2.Return += page2_Return;
this.NavigationService.Navigate(page2);
}
void page2_Return(object sender, ReturnEventArgs<String> e)
{
OnReturn(new ReturnEventArgs<String>(null));
}
Page2.Xaml:
<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="PQR"
x:TypeArguments="sys:String"
Title="page2">
...
</PageFunction>
void BackButton_Click(object sender, RoutedEventArgs e)
{
OnReturn(new ReturnEventArgs<String>(null));
}

Is it more efficient to build the "visual tree" in xaml rather than programmatically?

I’m working on a WPF application with a tabbed MDI interface. Basically, the user clicks a menu item and this creates and adds a new tab. A new 'MyUserControl' is created and set to be the tab's Content. Something like this:
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
this.TabControl.Items.Add(new TabItem() { Content = new MyUserControl() });
}
MyUserControl is composed of several nested controls (approx. 8 controls). When this approach is wired up to the actual control, the performance is unacceptable.
Unless I’m losing my mind, I’ve noticed that the performance hit seems to be much less when declaring the tab and content in xaml ahead of time and simply toggling the tab item's Visibility property:
<Controls:TabControl x:Name="TabControl" Grid.Row="1">
<Controls:TabControl.Items>
<Controls:TabItem x:Name="MyTabItem" Visibility="Collapsed">
<Controls:MyUserControl x:Name="MyUserControl" />
</Controls:TabItem>
</Controls:TabControl.Items>
</Controls:TabControl>
and
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
this.MyTabItem.Visibility = Visibility.Visible;
}
Can anyone explain this? Is it really more efficient to build the "visual tree" in xaml rather than programmatically? Or has the performance hit in my second approach just been moved to the overall form's load instead of when the menu item is clicked as in the first approach?
The second approach definitely seems to perform much better. Any thoughts?
It is no more efficient to declare it it xaml. I think you are correct in thinking that the performance hit has been moved to the form load.
If it is taking too long to load, maybe it is doing too much work in its constructor. See if you can minimize the work done during the loading of the control.
Otherwise, if the problem is just the sheer amount of controls in the user control, maybe you could keep a fully loaded tab in memory until it is ready to be used. Once the user clicks the menu item, add it to the tabcontrol and then start loading a new one in a background thread.
Yes, you moved the performance hit to the initialization of the Window. Even collapsed, the instance of the UserControl has been made - you've added it explicitly to the TabControl. Opening a tag in xaml is the same thing as saying new. Collapsed controls have to be constructed because even if you can't see it or interact with it on screen, other controls can have bindings to it and code-behind can work with the instance.
If you prefer your first approach, try caching the instance of the UserControl if you can instead of creating a new one each time.

WPF Ribbon - Hide quick access toolbar

how do you hide Quick Access Toolbar in a WPF's Ribbon?
For Microsoft Ribbon for WPF, you can hide it by using the VisualTreeHelper. On the Loaded event handler, just resize the row containing the Quick Access Toolbar to 0 :
private void RibbonLoaded(object sender, RoutedEventArgs e)
{
Grid child = VisualTreeHelper.GetChild((DependencyObject)sender, 0) as Grid;
if (child != null)
{
child.RowDefinitions[0].Height = new GridLength(0);
}
}
The Quick Access Toolbar is automatically hidden when the Ribbon control is in a RibbonWindow. When it is not, it seems impossible to hide it. I have already worked hours on this issue and was unable to hide it properly.
But there is one simple workaround: Place the Ribbon control inside of a Panel and give it a negative top margin so it will slide outside of the Panel. Set the Panel's ClipToBounds property to true and the QAT will be hidden.
By the way - there are multiple Ribbon implementations for WPF, even by Microsoft themselves ("Fluent Ribbon" and "Microsoft Ribbon for WPF"), so next time you should mention which one you are talking about.
Or if you want it all in the XAML, this works
<ribbon:Ribbon>
<ribbon:Ribbon.Loaded>CollapseQuickAccessToolbar</ribbon:Ribbon.Loaded>
<x:Code>
private void CollapseQuickAccessToolbar(Object sender, RoutedEventArgs e) {
((Grid)VisualTreeHelper.GetChild((DependencyObject)sender, 0)).RowDefinitions[0].Height = new GridLength(0);
}
</x:Code>
</ribbon:Ribbon>
Here is the solution :
this.ribbonControl1.ToolbarLocation = DevExpress.XtraBars.Ribbon.RibbonQuickAccessToolbarLocation.Hidden;
I know this is an old post, but found an easier solution...
Add this inside the ribbon :-
<ribbon:Ribbon.QuickAccessToolBar>
<ribbon:RibbonQuickAccessToolBar Visibility="Collapsed"/>
</ribbon:Ribbon.QuickAccessToolBar>
Bit late to the party.
<my:Ribbon >
<my:Ribbon.ApplicationMenu >
<my:RibbonApplicationMenu Visibility="Collapsed">
</my:RibbonApplicationMenu>
</my:Ribbon.ApplicationMenu>
This will help to hide the quick bar

Silverlight 4: Free Split Button

I'm looking for free splitbutton control for silverlight.
I've seen this blog however I cannot download it. Its blocked in my firewall.
Do you know any free splitbutton for silverlight?
Thank you
It is available for free in the Silverlight Toolkit.
Download the latest Silverlight Toolkit from CodePlex.
http://silverlight.codeplex.com/
Download the SplitButton Samples and Project. You may use the SplitButton project to compile your own version of the SplitButton.dll or use the Sample programs to study. (optional)
http://dlaa.me/Samples/SplitButton/SplitButton.zip
Add references (right click References) to the Silverlight toolkit and the SplitButton.dll in your Silverlight project.
SplitButton.dll
System.Windows.Controls.Input.Toolkit.dll
Add both namespaces to your XAML, for the Silverlight toolkit and the new SplitButton.
xmlns:splitButton="clr-namespace:Delay;assembly=SLTKSplitButton"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Add the Split Button code. This makes one button that drops to three options.
<splitButton:SplitButton x:Name='Button1' Content="Open" Click="Button1_Clicked">
<splitButton:SplitButton.ButtonMenuItemsSource>
<toolkit:MenuItem Header="Open" Click="Button1_Clicked" />
<toolkit:MenuItem Header="Open read-only" Click="Button1_ClickedRO" />
<toolkit:MenuItem Header="Open as copy" Click="Button1_ClickedAC" />
</splitButton:SplitButton.ButtonMenuItemsSource>
</splitButton:SplitButton>
Add Csharp code for click handlers for main button click or any of the three sub-option clicks.
private void Button1_Clicked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Opening document normally...");
}
private void Button1_ClickedRO(object sender, RoutedEventArgs e)
{
MessageBox.Show("Opening document read-only...");
}
private void Button1_ClickedAC(object sender, RoutedEventArgs e)
{
MessageBox.Show("Opening document as a copy...");
}
Give thanks to David Anson, a Microsoft developer who works with the Silverlight, Windows Phone, and WPF platforms. Twitter: #DavidAns

Resources