Using a collection of objects as a filter in a RadDataFilter

4 posts, 1 answers
  1. Phone
    Phone avatar
    5 posts
    Member since:
    Sep 2011

    Posted 14 Oct 2011 Link to this post

    Hello

    I have a Silverlight application in which I am using the RadDataFilter. This RadDataFilter uses a list of custom objects as the data source for the testComboBox. When a user clicks the "Test" button, the filter value is always set to "Unset". For instance, if I use the code below, I always see "Priority IsEqualTo <Unset>". However, if I use use a List of string values, everything works fine. Here is my code:

    <UserControl.Resources>
      <DataTemplate x:Key="priorityTemplate">
        <telerik:RadComboBox x:Name="testComboBox" MinWidth="100" DisplayMemberPath="Title" SelectedValue="{Binding Path=Value, Mode=TwoWay, FallbackValue=null}" SelectionChanged="testComboBox_SelectionChanged">
                    <telerik:RadComboBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel MinWidth="320" />
                        </ItemsPanelTemplate>
                    </telerik:RadComboBox.ItemsPanel>               
                </telerik:RadComboBox>
            </DataTemplate>
     
            <DataTemplate x:Key="locationTemplate"></DataTemplate>
     
            <DataTemplate x:Key="typeTemplate"></DataTemplate>
             
            <code:MyEditorTemplateSelector x:Key="myEditorTemplate">
                <code:MyEditorTemplateSelector.EditorTemplateRules>
                    <code:MyEditorTemplateRule PropertyName="Priority" DataTemplate="{StaticResource priorityTemplate}" />
                    <code:MyEditorTemplateRule PropertyName="Location" DataTemplate="{StaticResource locationTemplate}" />
                    <code:MyEditorTemplateRule PropertyName="TypeName" DataTemplate="{StaticResource typeTemplate}" />               
                </code:MyEditorTemplateSelector.EditorTemplateRules>
               </code:MyEditorTemplateSelector>
        </UserControl.Resources>
         
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
            </Grid.RowDefinitions>
             
            <Button Content="Test" Height="30" Width="90" Margin="0,0,0,8" HorizontalAlignment="Left" Click="Button_Click" />
            <telerik:RadDataFilter x:Name="filter" Grid.Row="1" EditorCreated="filter_EditorCreated" AutoGenerateItemPropertyDefinitions="False" EditorTemplateSelector="{StaticResource myEditorTemplate}" Loaded="filter_Loaded" />
        </Grid>

    Here is the relevant parts of the my code-behind:

    private void filter_Loaded(object sender, RoutedEventArgs e)
    {
        ItemPropertyDefinition priorityDefinition = new ItemPropertyDefinition("Priority", typeof(string), "Priority");
        filter.ItemPropertyDefinitions.Add(priorityDefinition);
    }
     
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string rules = string.Empty;
        foreach (FilterDescriptor description in filter.FilterDescriptors)
            rules += description.ToString() + "\n";
        MessageBox.Show(rules);
    }
     
    private void filter_EditorCreated(object sender, EditorCreatedEventArgs e)
    {
        switch (e.ItemPropertyDefinition.PropertyName)
        {
            case "Priority":
                // This works
                List<string> options = new List<string>() { "High", "Low" };
                //(RadComboBox)(e.Editor)).ItemsSource = options;
     
                // This doesn't
                List<Priority> priorities = new List<Priority>();
                priorities.Add(new Priority("High", true));
                priorities.Add(new Priority("Low", false));
                ((RadComboBox)(e.Editor)).ItemsSource = priorities;
     
                break;
        }
    }

    Here is the definition for Priority.cs:

    public class Priority
    {
        public string Title { get; set; }
     
        public bool Val { get; set; }
     
        public Priority(string title, bool val)
        {
            this.Title = title;
            this.Val = val;
        }
    }


    Finally, here is the Template selector code:

    public class MyEditorTemplateSelector : DataTemplateSelector
    {
        private List<MyEditorTemplateRule> templateRules = new List<MyEditorTemplateRule>();
        public List<MyEditorTemplateRule> EditorTemplateRules
        {
            get { return templateRules; }
        }
     
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            ItemPropertyDefinition propertyDefinition = (ItemPropertyDefinition)item;
            foreach (MyEditorTemplateRule rule in templateRules)
            {
                // Select the appropriate template for each property.
                if (rule.PropertyName == propertyDefinition.PropertyName)
                {
                    return rule.DataTemplate;
                }
            }
            return base.SelectTemplate(item, container);
        }
    }
     
    public class MyEditorTemplateRule
    {
        private string propertyName;
        public string PropertyName
        {
            get
            {
                return this.propertyName;
            }
            set
            {
                this.propertyName = value;
            }
        }
     
        private DataTemplate dataTemplate;
        public DataTemplate DataTemplate
        {
            get
            {
                return this.dataTemplate;
            }
            set
            {
                this.dataTemplate = value;
            }
        }
    }

    How do I use a Custom type for the filter options in a Editor Template?

    Thank you!
  2. Answer
    Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 19 Oct 2011 Link to this post

    Hello Phone,

    I'm attaching a sample application based on your code snippets, with a small change:

    private void filter_Loaded(object sender, RoutedEventArgs e)
    {
        ItemPropertyDefinition priorityDefinition = new ItemPropertyDefinition("Priority", typeof(Priority), "Priority");
        filter.ItemPropertyDefinitions.Add(priorityDefinition);
    }

    Let me know how this works on your end. Kind regards,
    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Phone
    Phone avatar
    5 posts
    Member since:
    Sep 2011

    Posted 24 Oct 2011 Link to this post

    Sweet!

    Thanks!
  5. Sean
    Sean avatar
    7 posts
    Member since:
    Mar 2009

    Posted 02 Nov 2011 Link to this post

    I am having a very similar problem to this one. Your examples above helped me get my collection of objects to show up in the filter, but when I select an item the value is always <UnSet>. I have gone through all my code and it all looks very similar to the rest of the code in this thread. One place where it differs a bit is in my DataTemplate. It looks like this:

    <DataTemplate x:Key="AccountingPeriodEditorTemplate">
                    <telerikComboBox:RadComboBox SelectedValue="{Binding Value, Mode=TwoWay}"
                                                 DisplayMemberPath="Name"
                                                 SelectedValuePath="ID"
                                                 IsEditable="True"
                                                 IsReadOnly="True"
                                                 ClearSelectionButtonContent="[Blank]"
                                                 ClearSelectionButtonVisibility="Visible"
                                                 Width="100"
                                                 telerik:StyleManager.Theme="{StaticResource CustomComboTheme}" />
                </DataTemplate>


    The RadComboBox is bound to an Observable collection of type "AccountingPeriod", which is an Entity class. I know the RadComboBox is bound correctly as I get a list of Accounting Periods in my drop down in the RadDataFilter. Do you have any ideas for this? Would it be helpful to see some other code section?

    Thanks,
    Sean
Back to Top