Jan 7 2010

Loading XAML at runtime when using external assemblies in Silverlight

Category: Silverlight | Telerik | Telerik ControlsBil@l @ 20:20

In an application I am working on, there is a need to load XAML dynamically from the server to populate a Telerik RadRibbonBar control. The XAML to be loaded is something similar to this:

<StackPanel xmlns:telerikRibbonBar="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.RibbonBar"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:cmd="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation">
    <telerikRibbonBar:RadRibbonBar x:Name="rbnBar"
                                   ApplicationName="VBC Office Tool"
                                   Margin="0"
                                   ApplicationButtonImageSource="../AppIcon.png">
        <telerikRibbonBar:RadRibbonTab Header="VBC">
            <telerikRibbonBar:RadRibbonGroup Header="Clipboard">
                <StackPanel Orientation="Horizontal">
                    <telerikRibbonBar:RadRibbonButton x:Name="btn1"
                                                      CollapseToSmall="Never"
                                                      Size="Large"
                                                      Tag="Module 1"
                                                      cmd:Click.Command="{Binding Path=RibbonCommand}"
                                                      cmd:Click.CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
                                                      Text="Paste">
                        <telerikRibbonBar:RadRibbonButton.Content>
                            <Image Source="../paste.png" />
                        </telerikRibbonBar:RadRibbonButton.Content>
                    </telerikRibbonBar:RadRibbonButton>
                    <telerikRibbonBar:RadRibbonButton x:Name="btn2"
                                                      HorizontalContentAlignment="Center"
                                                      CollapseToSmall="Never"
                                                      Size="Large"
                                                      Tag="Module 2"
                                                      cmd:Click.Command="{Binding Path=RibbonCommand}"
                                                      cmd:Click.CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
                                                      Text="Create Document">
                        <telerikRibbonBar:RadRibbonButton.Content>
                            <Image Source="../save.png" />
                        </telerikRibbonBar:RadRibbonButton.Content>
                    </telerikRibbonBar:RadRibbonButton>
                </StackPanel>
            </telerikRibbonBar:RadRibbonGroup>
        </telerikRibbonBar:RadRibbonTab>
        <telerikRibbonBar:RadRibbonTab Header="iRisk" />
    </telerikRibbonBar:RadRibbonBar>
</StackPanel>

 

At the client-side, I am adding the XAML into a ContentControl as follows:

System.Windows.Resources.StreamResourceInfo streamInfo =
    System.Windows.Application.GetResourceStream(new Uri("ContextMenuModule;component/View/RibbonBar.xaml", UriKind.Relative));
if ((streamInfo != null) && (streamInfo.Stream != null))
{
    using (System.IO.StreamReader reader = new System.IO.StreamReader(streamInfo.Stream))
    {
        UIElement uribbon = (UIElement)XamlReader.Load(reader.ReadToEnd());
        this.ribbon.Content = uribbon;
    }
}

On the XamlReader.Load() method, I was getting the following exception:

AG_E_PARSER_BAD_TYPE 

Before I give the solution that I found on a forums’ blog post (http://forums.silverlight.net/forums/t/44877.aspx), let me explain to you the environment where the above code is executing.

It is a Silverlight-Prism application where I have the following structure:

  1. Shell app
  2. Module app

 

The above code is executing inside the Module app and proper references are added to all Telerik assemblies.

Seems that in Silverlight, when loading XAML dynamically in a class library (A) or Silverlight application (B), and then that app or class library is used by another class library (C) or Silverlight app (D), then all external assemblies that were used by (A) and (B) should also be added to (C) and (D).

Therefore, all Telerik assemblies used by Module app should also be added to the Shell application.

 

Hope this helps,
Regards

Tags: , ,

Dec 23 2009

Visual Studio 2010 and Telerik Products

Category: Telerik Controls | visual Studio 2010Bil@l @ 10:07

Telerik products support for Visual Studio 2010 Beta 2

Visual Studio 2010 and Telerik Products

 

Hope this helps,
Regards

Tags: ,

Dec 10 2009

WebUI Test Studio 2.0 launches today

Category: Silverlight | Telerik Controls | WebUIBil@l @ 07:08

Read more here:

WebUI Test Studio 2.0 launches tomorrow (with webinar)

 

Hope this helps,
Regards

Tags: , ,

Dec 9 2009

Service Pack 1 released – RadControls for Silverlight/WPF Q3 2009

Category: Silverlight | Telerik ControlsBil@l @ 14:39

Telerik just released SP1 for the RadControls for Sivlerlight/WPF Q3 2009.

Read more here: Service Pack 1 released - RadControls for Silverlight/WPF Q3 2009

 

Hope this helps,
Regards

Tags: ,

Sep 21 2009

Telerik Sales Dashboard - Abide by Prism!

Category: Prism 2.0 | silverlight 2.0 | Telerik ControlsBil@l @ 22:32
There is no doubt that once you finish reading the Prism 2.0 (Composite Application Library) and try out all of the quick starts that accompany the guidance; it is a must to check out the Telerik Sales Dashboard. (http://demos.telerik.com/silverlight/salesdashboard)

Telerik not only pioneering in control development that targets the Microsoft .Net framework but also in educating developers all around the world with their extra efforts of:

  1. Code library
  2. Free software
  3. Extensive documentation
  4. Flash-Quick customer support
  5. Telerik labs
  6. Free Open-source projects

 

One of those major educating applications that Telerik delivers is the Telerik Sales Dashboard. It has been built on top of WPF and Silverlight keeping in mind the MVVM design pattern (Model-View-ViewModel) and Prism (Modularity, UI composition, global events, commanding, etc …)

You can download the source code to check it out and learn from it here (link)

In the whitepaper that accompanies in the Dashboard, I read the following:

PeriodSelector Module

This module handles the task of displaying a RadCalendar in a popup that can be used to select a range of dates for filtering data displayed in other modules. To close the popup containing the RadCalendar control when the user clicks outside of it, we subscribe to the MouseLeftButtonUp event of the RootVisual element of the application. We do not use the MouseLeftButtonDown event because it is used and handled internally by most controls and thus it is inappropriate for our purpose.

Also, we need to bind several properties of UI elements in this module to properties defined in the code-behind of the view, which are then in turn bound to properties in the viewmodel. This code-behind binding is necessary because the project requirements are not achievable with the XAML bindings available in Silverlight. This is a deviation from the Prism guidance, but, as already established, patterns should not be followed blindly if they block application requirements.

I found some code in the PeriodSelectorView.xaml.cs file that shouldn’t be present in a typical Prism application however, as it has been established in the Dashboard’s whitepaper, we shouldn’t follow a design pattern blindly if it blocks our work.

However, the code behind that was present could be replaced by implementing a new CommandBehavior that targets the RadCalendar. That was the first part of moving the code from code-behind into the PeriodSelectorViewModel.cs file. The other part is hooking up to the System.Windows.Application.Current.RootVisual.MouseLeftButtonUp  event inside the ViewModel file itself. The second part is easy and straight forward, so I will focus on the first one.


using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Data;
using System.Collections;

namespace Telerik.SalesDashboard.PeriodSelector
{
public partial class PeriodSelectorView : UserControl
{
public PeriodSelectorView(PeriodSelectorViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;

Binding b = new Binding();
b.Mode = BindingMode.TwoWay;
b.Path = new PropertyPath(&quot;CalendarSelectedDates&quot;);
this.SetBinding(PeriodSelectorView.SelectedDatesProperty, b);
calendar.SelectionChanged += (s, e) =&gt; this.SelectedDates = calendar.SelectedDates.ToList();
}

public IList SelectedDates
{
get
{
return (IList)this.GetValue(SelectedDatesProperty);
}
set
{
this.SetValue(SelectedDatesProperty, value);
}
}

// Using a DependencyProperty as the backing store for SelectedDates. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedDatesProperty =
DependencyProperty.Register(&quot;SelectedDates&quot;, typeof(IList), typeof(PeriodSelectorView), null);
}
}

What has been done above is:

  1. Define SelectedDates as a Dependency property on the View itself
  2. Define CalendarSelectedDates as a property on the ViewModel class
  3. Define a Binding between the above two properties
  4. Hooking up to the SelectionChanged event of the RadCalendar to fill the SelectedDates property hence filling the CalendarSelectedDates property.

 

The above was a way to hook into RadCalendar control on the View to retrieve selected dates and pass back the value to the ViewModel.

Now, what I did was:

     1. Removing all code-behind from the View

     2. Define a new DelegateCommand as follows:     

public ICommand CalendarSelectedCommand { get; private set; }
      3.  Instantiate the DelegateCommand as follows:
        this.CalendarSelectedCommand = new DelegateCommand<IList>(OnCalendarSelectionChanged);
private void OnCalendarSelectionChanged(IList args)
{
this.CalendarSelectedDates = args;
}

             4. Implemented a new RadCalendarCommandBehavior class.  As you know in Prism the CommandBevaior class does many things among which are:

a. Adds Commanding behavior into a Control

b. Defines the Command, CommandParameter and CommandBehavior attached properties that can be hooked to a Control.

c. Plays the role of proxy between Command definition and Control that the command is defined on where it attaches to a specific event of a Control and defines the event handler for that event, so that when the event is fired, the command is being called and executed.

Now, firing the event of a Control causes the attached event handler defined inside the CommandBehavior to fire. Usually this event handler calls the Execute method of the CommandBehavior that internally delegates the call to the Execute method on the Command it defines as a Dependency property.

Thus you can see how important the CommandBehavior class in attaching a Command behavior into a Control.

The Prism ships with the ClickCommandBehavior class only. When I saw the code written inside PeriodSelectorView.xaml.cs I realized that all what I need is create a RadCalendarCommandBehavior.cs class that attaches a new Command into the RadCalendar class by hooking into the RadCalendar.SelectionChanged event.

The purpose is to fire a command on the RadCalendar that can be handled inside the PeriodSelectorViewModel.cs class where I can easily retrieve the selected dates and hence no need for the mediator SelectedDates dependency property inside the View and no need for accessing the RadCalendar.SelectionChanged event. The goal is the same however, the “how” is the different and I believe using commands is more elegant!

I will not go into the details of creating the RadCalendarCommandBehavior class, the code is straight forward based on Prism knowledge


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using Telerik.Windows.Input;
using Telerik.Windows.Controls;
using System.Linq;
using System.Collections;

namespace Telerik.SalesDashboard.PeriodSelector
{
public class RadCalendarCommandBehavior : CommandBehaviorBase<RadCalendar>
{
public RadCalendarCommandBehavior(RadCalendar source)
: base(source)
{
source.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(source_SelectionChanged);
}

void source_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
{
var calendar = sender as RadCalendar;
if (calendar != null)
{
this.Command.Execute(calendar.SelectedDates.ToList());
}
}
}

public static class SelectionChanged
{
private static readonly DependencyProperty SelectionChangedCommandBehaviorProperty = DependencyProperty.RegisterAttached(
"SelectionChangedCommandBehavior",
typeof(RadCalendarCommandBehavior),
typeof(SelectionChanged),
null);

public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(SelectionChanged),
new PropertyMetadata(OnSetCommandCallback));

public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
"CommandParameter",
typeof(object),
typeof(SelectionChanged),
new PropertyMetadata(OnSetCommandParameterCallback));


public static void SetCommand(RadCalendar calendar, ICommand command)
{
calendar.SetValue(CommandProperty, command);
}

public static ICommand GetCommand(RadCalendar calendar)
{
return calendar.GetValue(CommandProperty) as ICommand;
}

public static void SetCommandParameter(RadCalendar calendar, object parameter)
{
calendar.SetValue(CommandParameterProperty, parameter);
}

public static object GetCommandParameter(RadCalendar calendar)
{
return calendar.GetValue(CommandParameterProperty);
}

private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadCalendar calendar = dependencyObject as RadCalendar;
if (calendar != null)
{
RadCalendarCommandBehavior behavior = GetOrCreateBehavior(calendar);
behavior.Command = e.NewValue as ICommand;
}
}

private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
RadCalendar calendar = dependencyObject as RadCalendar;
if (calendar != null)
{
RadCalendarCommandBehavior behavior = GetOrCreateBehavior(calendar);
behavior.CommandParameter = e.NewValue;
}
}

private static RadCalendarCommandBehavior GetOrCreateBehavior(RadCalendar calendar)
{
RadCalendarCommandBehavior behavior = calendar.GetValue(SelectionChangedCommandBehaviorProperty) as RadCalendarCommandBehavior;
if (behavior == null)
{
behavior = new RadCalendarCommandBehavior(calendar);
calendar.SetValue(SelectionChangedCommandBehaviorProperty, behavior);
}

return behavior;
}
}
}

5. What is left is to add the following into the PeriodSelectorView.xaml:

a.       Add a new XML namespace reference into the header of the UserControl:

xmlns:MyCommands="clr-namespace:Telerik.SalesDashboard.PeriodSelector"
  <telerikInput:RadCalendar …  MyCommands:SelectionChanged.Command="{Binding CalendarSelectedCommand}"   … />

That’s all what you need to do to remove the code-behind from the PeriodSelectorView into the PeriodSelectorViewModel !

 

If you find that the above can be improved, or anything you find you couldn’t understand, or you think is not suitable, etc … Please don’t hesitate to contact me at: bhaidar @ gmail.com

Regards

 

Tags: , ,

Jun 15 2009

Step-by-step Tutorial – RadControls for WinForms

Telerik provides a new step by step tutorial on Rad Controls for WinForms.

Check it out here:  Step-by-Step Tutorial - RadControls for WinForms

 

Enjoy it,
Regards

Tags: ,

May 30 2009

Telerik Sales Dashboard Demo for Silverlight and WPF

Category: silverlight 2.0 | Telerik Controls | WPFBil@l @ 20:52

Telerik just release the Telerik Sales Dashboard Demo for both Silverlight and WPF.

Check this blog post for more info on the Dashboard: Telerik Dashboard for Silverlight and WPF

 

Hope you enjoy it!
regards

Tags: , ,

May 29 2009

Telerik WebUI Test Studio

I want to invite you to check the article titled "Telerik WebUI Test Studio" on the http://www.aspnetpro.com magazine that is authored by my colleague Rawane Madi.

The article can be reached at: Telerik WebUI Test Studio

 

Good work Rawane! Keep it up.

Regards

Tags: ,

Apr 15 2009

Silverlight Release History : Q1 2009 SP1 (version 2009.1.413)

Category: silverlight 2.0 | Telerik ControlsBil@l @ 09:20

Q1 2009 SP1 (version 2009.1.413)

 

 

SilverlightGridView Icon

RadGridView for Silverlight

New Features:

  • CanUserSortColumns property added
  • Disabled CheckBox for boolean column in view mode instead True/False added
  • Cells property added for grid rows - Items is now obsolete.
  • ExcelML export added
  • Exporting improvements (styles & formatting)
  • Exporting event added
  • Support for mouse over state in Cells added
  • GridViewComboboxColumn added
  • DistinctValues IValueConverter support added
  • RowAppearance settings moved to GridViewDataControl (RowStyle, HeaderRowStyle, etc.) - RowAppearance is now obsolete
  • HeaderRowStyle property added for GridViewDataControl
  • GroupRowStyle property added for GridViewDataControl
  • NewRowStyle property added for GridViewDataControl
  • Added Validation style for Vista, Summer and Office_Black themes
  • TAB naviagation improvements
  • CellTemplate and CellEditTemplate DataTemplate properties for GridViewColumn added
  • CreateCellElement and CreateCellEditElement virtual methods for GridViewColumn added

Fixes:

  • Export does not take into account columns that are hidden is now fixed
  • Height cannot be set in XAML for the grid rows is now fixed
  • Duplicate rows in Export to Excel is now fixed
  • HeaderText cannot be set in runtime is now fixed
  • GridViewColumn IsVisible now will not reset GridViewDataControl template
  • Cells are not created if the grid is not visible
    Control+Click multiple selection broken after switching tabs in a TabControl is now fixed
  • Column reordering with custom filter description added to the composite filter description of the grid is now fixed
  • DataElement property of RowLoadedEventArgs throws exception on new row is now fixed
  • Async binding does not work properly with filtering
  • Sorting on nested or indexed properties is now fixed
  • Distinct values on ntext columns exception is now fixed
  • Sorting on ntext columns is now fixed
  • CustomTypeDescriptor problems with sorting and distinct values are now fixed (WPF only)
  • Filtering on string when target value is null is now fixed
  • GroupingRequested not raised on group panel cell close button click is now fixed
  • DataFormatString does not work properly on export is now fixed
  • Vertical scrollbar margin not calculated properly when new row is visible is now fixed
Silverlight RadChart Icon

RadChart

New Features:

  • Added two new 2D chart types: StackedLine and StackedSpline
  • Added AxisY.ExtendDirection property. This property will define if and in which direction AxisY will automatically add an additional step in order to ensure there is always a gap between the graph and the chart area top/bottom end. It is respected only when AutoRange property is set to true and IsZeroBasedProperty is treated with higher priority. The possible values are:

    • AxisExtendDirection.None - the Y axis will not be extended
    • AxisExtendDirection.Down - the Y axis will have an additional item at the bottom
    • AxisExtendDirection.Up - the Y axis will have an additional item at the top
    • AxisExtendDirection.Both - the Y axis will have an additional item at both - top and bottom
    • AxisExtendDirection.Smart - acts like AxisExtendDirection.Both with a single difference: the axis will never be extended outside the 0 value
  • New series types - StackedLine and StackedSpline
  • Exposed Style properties for customization of the visual elements of RadChart:

    • RadChart.TitleStyle - gets or sets the title style
    • RadChart.LegendStyle - gets or sets the legend style
    • SeriesDefinition.PointMarkItemStyle - gets or sets the Style associated with each point mark (where applicable)
    • SeriesDefinition.SeriesItemLabelStyle - gets or sets the series item label style
    • ChartArea.AxisXStyle - gets or sets the axis X style
    • ChartArea.AxisYStyle - gets or sets the axis Y style
    • ChartLegend.LegendItemStyle - gets or sets the legend item style
  • PointMarks shape and appearance can now be customized through
  • SeriesDefinition.Appearance.PointMark complex property
  • Added item tooltip format
  • Introduced automatic AxisX.LayoutMode selection that takes the actual displayed chart series into account

Changes:

  • Fixed an issue with series item format not working for the last item
  • Applying styles to AxisXLabel2D is now possible
  • Fixed an issue with wrong ItemIndex in ChartItemClickEventArgs for radial series.

Breaking Changes:

  • AxisY.AutoScale is now obsolete. Please, use AutoRange property instead
  • Numeric label formats have changed - there is no {0} placeholder used anymore. For example the old format "{0:C}" is now written as "C"

RadGauge

New Features:

  • Semi-circle styles for radial gauges
  • Quadrant-like styles for radial gauges
Silverlight RadEditor Icon

RadEditor

Fixes in Firefox:

  • Telerik.Windows.Controls.Editor.dll – fixed mistaken assembly info affecting IntelliSense
SilverlightMaskedTextBox Icon

RadMaskedTextBox

  • SelectionStart, SelectionLength, AutoCompleteInterval - new properties added
RadPanelBar

RadPanelBar

  • Accordion functionality
RadDragAndDropManager Icon

RadDragAndDropManager

  • Drag-Related properties can now be set in the constructor of the RootVisual, no Null Exceptions will occur
  • When the drag Source is set as a DragCue, it is correctly put back in the visual tree on drop complete
  • Drag-drop across multiple nested windows now works as expected
RadTabControl Icon

RadTabControl

  • TemplateBinding to HeaderTemplate now added in all templates
RadWindow Icon

RadWindow

  • Modal Window background now visible in Alert, Prompt, Confirm.
RadMediaPlayer Icon

RadMediaPlayer

Fixes:

  • Fixed RadMediaPlayer bug affecting the CurrentItem property when set before the template has been applied
  • Removed a Loaded Trigger which confused Blend during template editing

Memory Leak Fixes:

  • RadWindow, RadTreeView, RadMenu, RadNumericUpDown, RadSlider

Tags: ,

Apr 11 2009

The imported project &quot;C:\Program Files\MSBuild\Microsoft\StyleCop\v4.3\Microsoft.Style.Targets&quot; was not found ....

Category: Telerik Controls | Visual Studio 2008Bil@l @ 17:35

I was trying to open a Project for one of the Telerik source code projects and got the above error.

All what I had to do is just make sure VS SP1 is installed. That's it.

 

Hope this helps you,
Regards

Tags: ,