Telerik blogs

Attached behaviors give you the opportunity to go far beyond the build-in functionality and to create one of your own. You are free to associate an action with an event or a property value. All you need is the assembly that comes with Expression Blend - System.Windows.Interactivity.dll.
In our case here the goal will be to create a behavior that enables us to bind to the property of the RadGridView’s rows – IsExpandable. Normally you do not need to do nothing more but to set it to the value you want. However, if it is necessary to use it after the grid is loaded, you have to add some more lines of code in the application to make it work. Here the scenario will be to load the data asynchronously and to show the button for expanding only if there is items in the RowDetailsTemplate. The resulted RadGridView should be the following:

Capture

 

Let us begin with the Behavior - IsExpandableBehavior.cs

Firstly, the thing to be done is to inherit the generic class Behavior<T>. T is a dependency object, most often FrameworkElement or UIElement. In this case it will be RadGridView. 

Then you need to define what you want to do when attaching and detaching the behavior. As the IsExpandable property belongs to the GridViewRow the two methods OnAttached() and OnDetached() will listen to the events RowLoaded and RowUnloaded.

protected override void OnAttached()

{

    base.OnAttached();

 

    this.AssociatedObject.RowLoaded += OnRowLoaded;

    this.AssociatedObject.RowUnloaded += OnRowUnloaded;

}

 

protected override void OnDetaching()

{

    base.OnDetaching();

 

    this.AssociatedObject.RowLoaded -= OnRowLoaded;

    this.AssociatedObject.RowUnloaded -= OnRowUnloaded;

}

 

Consequently, the two methods called are the OnRowLoaded and OnRowUnloaded that will be responsible for setting and clearing the binding.

private void OnRowLoaded(object sender, RowLoadedEventArgs e)

{

    var row = e.Row as GridViewRow;

 

    if (row != null)

    {

       row.SetBinding(GridViewRow.IsExpandableProperty, new Binding(this.IsExpandableSourcePropertyName) { Converter = converter });

    }

}

 

private void OnRowUnloaded(object sender, RowUnloadedEventArgs e)

{

    var row = e.Row as GridViewRow;

 

    if (row != null)

    {

       BindingOperations.ClearBinding(row, GridViewRow.IsExpandableProperty);

    }

}

 

The used converter here is a simple IntegerToBooleanConverter that implements the IValueConverter Interface.
And one last, but still important detail here is to define the property corresponding with the newly created behavior and to register its name.

 public string IsExpandableSourcePropertyName

 {

      get

      {

          return (string)GetValue(IsExpandableSourcePropertyNameProperty);

      }

      set

      {

          SetValue(IsExpandableSourcePropertyNameProperty, value);

      }

 }

 

 public static readonly DependencyProperty IsExpandableSourcePropertyNameProperty =

            DependencyProperty.Register("IsExpandableSourcePropertyName", typeof(string), typeof(IsExpandableBehavior), new PropertyMetadata(null));

 

Once you are ready with the code of the behavior, all you need to do is to attach it in xaml:

   <i:Interaction.Behaviors>

           <my:IsExpandableBehavior IsExpandableSourcePropertyName="Players.Count" />

   </i:Interaction.Behaviors>

 

Do not forget to add reference to the assembly System.Windows.Interactivity.dll.

   xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

 

That’s it!

You can download the sample projects for Silverlight and WPF accordingly.

 


About the Author

Maya Zhecheva

 is Software Developer in Telerik XAML Team

Comments

Comments are disabled in preview mode.