Conditional formatting of RadPanelBar items

9 posts, 2 answers
  1. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 09 Jul 2009 Link to this post

    Hi

    My RadPanelBar is bound to a collection of objects like this:

    XAML:
    <HierarchicalDataTemplate x:Key="hdt" ItemsSource="{Binding ClientPublications}">                         
                <TextBlock Text="{Binding Name}"></TextBlock> 
    </HierarchicalDataTemplate> 
     
    <telerik:RadPanelBar x:Name="reportMenuPanelBar" Grid.Row="1" Margin="1,0,1,1" ItemsSource="{Binding}" ItemTemplate="{StaticResource hdt}" Selected="reportMenuPanelBar_Selected"
    </telerik:RadPanelBar> 

    C#:
    IList<ClientReport> reports = mgr.GetAllClientReports(); 
    reportMenuPanelBar.DataContext = reports; 

    I want the textweight for the child panel bar items to be different (i.e. bold or normal) depending on the value of an object's property.

    How can I do this?

    Thanks.
  2. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 09 Jul 2009 Link to this post

    Hello Ryan,

    You can use a TemplateSelector in your scenario. Please take a look at the attached sample project. The sample is using a RadTreeView control but the usage of a TemplateSelector is identical with a RadPanelBar control.

    Greetings,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 10 Jul 2009 Link to this post

    Thanks Tihomir, but unfortunately I cannot open the TreeView_TemplateSelector project. It is not supported by my version of VS.

    I using VS 2008 no SP.

    Regards
    Ryan
  5. Answer
    Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 13 Jul 2009 Link to this post

    Hi Ryan,

    Is there any specific reason why you are not using the SP for Visual Studio 2008? It is generally desireable to use the latest updates and service packs because of all the bug fixes and support for the latest dev tools.

    In case you don't want to upgrade your installation of Visual Studio, you can refer to the following blog post which discusses data binding in WPF and has a section demonstrating how to use TemplateSelectors:

    http://blogs.telerik.com/deyanvarchev/posts/08-06-17/wpf_data_binding_unraveling_data_templates.aspx

    Let me know if the article helps.

    All the best,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 14 Jul 2009 Link to this post

    Hi Tihomir

    I am in fact using SP1 (I was told otherwise, but never mind).

    I am using VS2008 Pro 9.0.30729.1

    I still can't open the project.
  7. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 14 Jul 2009 Link to this post

    Hello Ryan,

    You probably do not have the requirements for Silverlight development installed on your machine (runtime, SDK, tools for VS). Did you read the article I refered to in my last post? It explains how to use TemplateSelectors in WPF. If you read the article there will be no need to check the sample Silverlight project I sent originally.

    Sincerely yours,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 22 Jul 2009 Link to this post

    Hi Tihomir

    Thank you for the information you have provided so far. It has helped.

    However it is not working quite right for me, probably because I have not implemented properly.
    The datasource is a collection of Report objects andeEach Report object may contain a collection of Publication objects.



    There are no RadPanelBarItems (Publications) showing. Only RadPanelBar's (Reports) are showing.

    What am I doing wrong?

    XAML:
    <Style x:Key="UnreadItemStyle" TargetType="TextBlock"
                <Setter Property="FontWeight" Value="Bold" ></Setter
            </Style> 
            <Style x:Key="ReadItemStyle" TargetType="TextBlock"
                <Setter Property="FontWeight" Value="Normal"></Setter> 
    </Style>         
             
    <HierarchicalDataTemplate x:Key="unreadPublications" ItemsSource="{Binding ClientPublications}">             
                <TextBlock Text="{Binding Name}" Style="{StaticResource UnreadItemStyle}"></TextBlock> 
    </HierarchicalDataTemplate> 
    <HierarchicalDataTemplate x:Key="readPublications" ItemsSource="{Binding ClientPublications}"
                <TextBlock Text="{Binding Name}" Style="{StaticResource ReadItemStyle}"></TextBlock> 
    </HierarchicalDataTemplate> 
    <local:CustomDataTemplateSelector x:Key="customSelector"></local:CustomDataTemplateSelector> 
     
    <telerik:RadPanelBar x:Name="reportMenuPanelBar" Grid.Row="1" Margin="1,0,1,1"  
                                    ItemTemplateSelector="{StaticResource customSelector}" 
                                    ItemsSource="{Binding}" 
                                    Selected="reportMenuPanelBar_Selected"
                </telerik:RadPanelBar> 

    CustomDataTemplateSelector:
    public class CustomDataTemplateSelector : DataTemplateSelector 
        { 
            public override DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) 
            { 
                var reports = item as ClientReport; 
                IList<ClientPublication> pubs = reports.ClientPublications; 
                ContentPresenter presenter = container as ContentPresenter; 
                if (presenter != null && pubs != null
                {    
                    foreach (var pub in pubs) 
                    { 
                        if (pub != null && pub.HasBeenRead == true
                        { 
                            return presenter.TryFindResource("readPublications"as DataTemplate; 
                        } 
                        else 
                        { 
                            return presenter.TryFindResource("unreadPublications"as DataTemplate; 
                        } 
                    }                 
                } 
     
                return base.SelectTemplate(item, container); 
            } 
     
        } 

    XMAL.CS:

    IList<ClientReport> reports = mgr.GetAvailableClientReports();                         
    reportMenuPanelBar.DataContext = reports;  


  9. Answer
    Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 22 Jul 2009 Link to this post

    Hi Ryan,

    Your mistake was that in the TemplateSelector you were assuming the item would always be a ClientReport, so when it was the turn of ClientPublications to get their templates they were not getting any.

    I prepared an example that demonstrates how to get the selector to work in your scenario. Please take a look and let me know if my solution works for you.

    Sincerely yours,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 23 Jul 2009 Link to this post

    Thanks for your help Tihomir!
Back to Top
UI for WPF is Visual Studio 2017 Ready