使用对象集合作为RadDataFilter中的过滤器

时间:2011-10-17 12:49:35

标签: c# silverlight telerik

我有一个Silverlight应用程序,我在其中使用RadDataFilter。此RadDataFilter使用自定义对象列表作为testComboBox的数据源。当用户单击“测试”按钮时,过滤器值始终设置为“未设置”。例如,如果我使用下面的代码,我总是看到“Priority IsEqualTo”。但是,如果我使用一个字符串值列表作为testComboBox的数据源,一切正常。这是我的代码:

<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>

以下是我的代码隐藏的相关部分:

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;
    }
}

以下是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;
    }
}

最后,这是模板选择器代码:

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;
        }
    }
}

如何在编辑器模板中为过滤器选项使用自定义类型?

谢谢!

2 个答案:

答案 0 :(得分:1)

实际上,我认为当你使用字符串列表时,你会使用以下代码:

<telerik:RadComboBox x:Name="testComboBox" MinWidth="100" DisplayMemberPath="Title"
SelectedValue="{Binding Path=Value, Mode=TwoWay, FallbackValue=null}"
SelectionChanged="testComboBox_SelectionChanged">

但是,当您更改DataSource时,您使用的是对象集合,那么您可能会更改DisplayMemberPathSelectedValue。 对于DadDataFilter,您不应该更改

 selectValue={Binding Path=Value, Mode=TwoWay, FallbackValue=null};

此处path=Value必须与所有绑定相同。 尝试使用以下内容:

<telerik:RadComboBox x:Name="testComboBox" MinWidth="100" DisplayMemberPath="Title"
SelectedValue="{Binding Value, Mode=TwoWay, FallbackValue=null}"
SelectionChanged="testComboBox_SelectionChanged">

答案 1 :(得分:0)

我在WPF中遇到了完全相同的问题,在google搜索我最终想出的解决方案时发现了你的问题:

您将ItemPropertyDefinition的属性类型设置为字符串,但在RadComboBox的{​​{1}}的绑定中,绑定对象值的类型为“优先级”。因此,解决方案是在绑定中使用SelectedValue对象,Converter类必须实现Converter

IValueConverter

这是我的xaml:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using QUPS.Data;
using QUPS.Data.QUPSModel;

namespace QUPS.Helpers
{
    public class EntityObjectToDecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            if (value is IQUPSEntityObject)
            {
                var rec = (IQUPSEntityObject)value;
                return rec.ID;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            if (value is IQUPSEntityObject)
            {
                var rec = (IQUPSEntityObject)value;
                return rec.ID;
            }

            return value;
        }
    }
}