Home   Subscribe   Linkedin
  Archive Contact  

Adding Dynamic Cell Style to RadGridView for Silverlight

Lets say we have a list of products and we need to mark higher prices (>15.00)  with a red color. Something like :

We have to create a small IValueConverter. It will take care to convert the value of the price to the appropriate color:

public class PriceToColorConverter : IValueConverter

      {

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            {

                SolidColorBrush brush = new SolidColorBrush();

                decimal price = (decimal) value;

     

                if (price > 15)

                   brush.Color = Colors.Red;

               else

                   brush.Color = Colors.Green;

    

               return brush;

           }

    

           public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

           {

              throw new NotImplementedException();

           }

       }

We use this converter to bind the Foreground property of the cell to the product’s Price :

<Grid>
      <Grid.Resources>

              <local:PriceToColorConverter x:Key="MyConverter" />

         </Grid.Resources>

         <telerik:RadGridView x:Name="RadGridView1" AutoGenerateColumns="False" >

             <telerik:RadGridView.Columns>

                 <telerik:GridViewDataColumn DataMemberBinding="{Binding ProductName}" Header="Product" />

                  <telerik:GridViewDataColumn DataMemberBinding="{Binding Price}" Header="Price">

                      <telerik:GridViewDataColumn.CellStyle>

                         <Style TargetType="telerik:GridViewCell">

                             <Setter Property="Foreground" Value="{Binding Price, Converter={StaticResource MyConverter}}" />

                         </Style>

                      </telerik:GridViewDataColumn.CellStyle>

                  </telerik:GridViewDataColumn>

             </telerik:RadGridView.Columns>

        </telerik:RadGridView>

      </Grid>
Posted by: Mohd Ishaq

Categories: Silverlight, Telerik

Tags: , , , ,

Communication between Silverlight and Javascript

While working with one of the issues in my project where I need to have some communication between the Silverlight and Asp.Net page (.aspx page),mainly regarding passing some silverlight variable value to asp.net part.I came across several complex posts for that ,so i took an optimum solution from them and created the following post.This is a very simple example where we can call both ways(from asp to Silverlight and vice versa).

 

Silverlight
Here UpdateText is a method in Silverlight which will be called from javascript in xyz.aspx page. Remember to use [ScriptableMember] tag before the portion you want to expose to Javascript. 'TalkToJavaScript' is method in Javascript which we are calling with 'Hello From SL' is a string which we are passing to it.

[ScriptableMember]
 public void UpdateText(string result)
        {
            MessageBox.Show(result);
            myTextBox.Text = result;

        }
         [ScriptableMember]
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.RegisterScriptableObject("Page"this);
            HtmlPage.Window.Invoke("TalkToJavaScript""Hello From SL");
          
        }

Javascript

Here 'Sl' is silverlight object id
( <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
 width="100%" height="100%" id="Sl">)
& 'UpdateText' is a same method we defined in Silverlight


function TalkToJavaScript(data) {

            alert("Message received from Silverlight: " + data);
            var control = document.getElementById("Sl");
            control.Content.Page.UpdateText("Hello from Javascript!");
        } 

 SIlverlight calling JavaScript

JavaScript calling SIlverlight


I have attached a sample project.

SLjavascript.zip (18.79 kb)

 

Posted by: Rishabh Toshniwal

Categories: ASP.NET, Javascript, Silverlight

Tags: