Is it possible to create style and assign at code behind

7 posts, 0 answers
  1. charan
    charan avatar
    7 posts
    Member since:
    Jul 2010

    Posted 05 Sep 2011 Link to this post

    Hi Telerik,

    In project i have one requirement which i need to implement if there is possibility.

    Need to save the Xaml code (Style or some xaml code) into the Db at one column as string.

    xaml code (i.e., as string)  is retreived and converted as xaml code and need to apply the style or add xaml code into scheduleview.

    Can we apply style or add the xaml code to the ScheduleView from Codebehind (e.g., from ViewModel)  without adding into the page.resources ?

    If it is possible could you please share an example with explaination.

    Please provide me solution as early as possible.


    Regards,

    Charan
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 06 Sep 2011 Link to this post

    Hi Charan,

    You can set the style for example like this:
    myScheduleView.Style = Application.Current.Resources["ScheduleView"] as Style;

    And instead of the Resources of the application you can use the value of the property from your ViewModel.

    Please let us know if that worked for you.

    Best wishes,
    Konstantina
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. charan
    charan avatar
    7 posts
    Member since:
    Jul 2010

    Posted 06 Sep 2011 Link to this post

    Hi Konstantina,

    Thanks for your immediate reply.

    I know about the solution which you had suggested .

    Actually you didn't get my requirement.

    I have one string column at DB which save the xaml code (e.g., <Grid Background = "Red"> </Grid>)

    I will retreive this string and get xaml code as shown in below

     

     

    Grid grid = (Grid)XamlReader.Load(staff.StaffXamlCode);

     

    Here  is the main thing which i need to get help from Telerik.

    How i can add this grid into the AppointmentItem. (To set the background colour). ?

    Similarly i will do with the Style also (As explained above).

    "The main goal is to generate the dynamic styles or resources for the RadScheduleView"

    Please provide the solution as early as possible.

    If your not clear with my requirement please ask me i will tell little more detail.

    regards,

    Charan


  4. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 07 Sep 2011 Link to this post

    Hi Charan,

    If I have understood you correctly, you need to change the background of the AppointmentItems? If so, you might want to consider using the AppointmentStyleSelector of the ScheduleView. For an example please refer to this online demo.

    Hope this helps.

    Greetings,
    Konstantina
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  5. charan
    charan avatar
    7 posts
    Member since:
    Jul 2010

    Posted 07 Sep 2011 Link to this post

    Hi Konstantina ,

    Thanks for your reply.

    yes your right i need to changed the Background of AppointmentItem.But not using AppointmentStyleSelector.

    My requirement is i will get the xaml code as string  from DB.

    e.g.,I have Staff table

    Columns are : StaffID,StaffName,StaffXamlCode

    StaffXamlCode  has 

     

    "<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +

     "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +

     "xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk' Background = 'Red' >" +

     "<TextBlock Text='Name' Foreground ='Green' />" +

     "</Grid>";

     At code behind i will load the string using XamlLoader

    Grid grid = (Grid)XamlReader.Load(staff.StaffXamlCode);

    One more thing similarly i might have Style also.

    (i.e. staff.StaffXamlCode = "<Style x:Key='ShortConsultStyle' " +

     

     "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +

     "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +

     "xmlns:telerik='http://schemas.telerik.com/2008/xaml/presentation'" +

      " TargetType='telerik:AppointmentItem'>" +

      

    "<Setter Property='Background' Value='Yellow' />" +

     "<Setter Property='BorderBrush' Value='Blue' />" +

     "<Setter Property='Padding' Value='5' />" +

     "<Setter Property='Margin' Value='1 0 1 0' />" +

     "<Setter Property='Foreground' Value='Black' />" +

      "</Style>";
     

    Now i have the xamlcode(Grid or Style).How can i append/insert or apply to the AppointmentItem at code behind?

    Here i am not using AppointmentStyleSelector and will not add into page resources.

    Goal is  Appointment should be displayed with there own sytle or with own fields as like the user saved in DB.
     
    Please provide me the best solution.

    regards,

    Charan
  6. Pana
    Admin
    Pana avatar
    748 posts

    Posted 14 Sep 2011 Link to this post

    Hi,

    To apply styles to the AppointmentItems you will need a StyleSelector no mater how hard you try to avoid it. In its SelectStyle override you could just:

    return (Style)XamlReader.Load(staff.StaffXamlCode)

    Note however that the styles will have to set control templates.

    As for the Grid you may take the same approach only using the AppointmentItemContentTemplateSelector and in the selector's SelectTemplate you could:

    return (DataTemplate)XamlReader.Load(staff.StaffXamlCode)

    This would be better approach since you will not bother with the Style which includes 2 different ControlTemplates based on the orientation etc.

    Note that the Grid in the xaml example you have provided below would have to be wrapped in DataTemplate.

    Kind regards,
    Pana
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  7. charan
    charan avatar
    7 posts
    Member since:
    Jul 2010

    Posted 14 Sep 2011 Link to this post

    Hi Pana,

    Thanks for your reply.

    I had completed my task already as like the instructions which you had suggested.

    Regards,

    Charan
Back to Top