Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在RadDataFilter中将对象集合用作筛选器_C#_Silverlight_Telerik - Fatal编程技术网

C# 在RadDataFilter中将对象集合用作筛选器

C# 在RadDataFilter中将对象集合用作筛选器,c#,silverlight,telerik,C#,Silverlight,Telerik,我有一个Silverlight应用程序,其中我使用RadDataFilter。此RadDataFilter使用自定义对象列表作为testComboBox的数据源。当用户单击“测试”按钮时,过滤器值始终设置为“未设置”。例如,如果我使用下面的代码,我总是看到“Priority IsEqualTo”。但是,如果我使用字符串值列表作为testComboBox的数据源,那么一切正常。这是我的密码: <UserControl.Resources> <DataTemplate x:Ke

我有一个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>
最后,这里是模板选择器代码:

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;
        }
    }
}
公共类MyEditorTemplateSelector:DataTemplateSelector
{
私有列表模板规则=新列表();
公共列表编辑器模板规则
{
获取{return templateRules;}
}
公共覆盖数据模板SelectTemplate(对象项,DependencyObject容器)
{
ItemPropertyDefinition propertyDefinition=(ItemPropertyDefinition)项;
foreach(模板规则中的MyEditorTemplateRule规则)
{
//为每个属性选择适当的模板。
if(rule.PropertyName==propertyDefinition.PropertyName)
{
返回规则.DataTemplate;
}
}
返回基地。选择模板(项目、容器);
}
}
公共类MyEditorTemplateRule
{
私有字符串propertyName;
公共字符串PropertyName
{
得到
{
返回this.propertyName;
}
设置
{
this.propertyName=值;
}
}
私有数据模板数据模板;
公共数据模板数据模板
{
得到
{
返回此.dataTemplate;
}
设置
{
this.dataTemplate=值;
}
}
}
如何为编辑器模板中的过滤器选项使用自定义类型


谢谢大家!

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

<telerik:RadComboBox x:Name="testComboBox" MinWidth="100" DisplayMemberPath="Title"
SelectedValue="{Binding Path=Value, Mode=TwoWay, FallbackValue=null}"
SelectionChanged="testComboBox_SelectionChanged">
此处的
path=Value
必须与所有绑定相同。 尝试使用以下方法:

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

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

<telerik:RadComboBox x:Name="testComboBox" MinWidth="100" DisplayMemberPath="Title"
SelectedValue="{Binding Path=Value, Mode=TwoWay, FallbackValue=null}"
SelectionChanged="testComboBox_SelectionChanged">
此处的
path=Value
必须与所有绑定相同。 尝试使用以下方法:

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

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

您将
ItemPropertyDefinition
的属性类型设置为string,但在
RadComboBox
SelectedValue
的绑定中,绑定对象值的类型为“Priority”。因此,解决方案是在绑定中使用
Converter
对象,
Converter
类必须实现
IValueConverter

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;
        }
    }
}
这是我的xaml的一部分:

<DataTemplate x:Key="CriteriaRightExpressionComboBoxTemplate">
    <telerik:RadComboBox SelectedValue="{Binding Value, Mode=OneWayToSource, FallbackValue=null, Converter={StaticResource EntityObjectToDecimalConverter}}"
                             MinWidth="100"
                             ItemTemplateSelector="{StaticResource CriteriaRightExpressionComboBoxTemplateSelector}"
                             >
    </telerik:RadComboBox>
</DataTemplate>

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

您将
ItemPropertyDefinition
的属性类型设置为string,但在
RadComboBox
SelectedValue
的绑定中,绑定对象值的类型为“Priority”。因此,解决方案是在绑定中使用
Converter
对象,
Converter
类必须实现
IValueConverter

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;
        }
    }
}
这是我的xaml的一部分:

<DataTemplate x:Key="CriteriaRightExpressionComboBoxTemplate">
    <telerik:RadComboBox SelectedValue="{Binding Value, Mode=OneWayToSource, FallbackValue=null, Converter={StaticResource EntityObjectToDecimalConverter}}"
                             MinWidth="100"
                             ItemTemplateSelector="{StaticResource CriteriaRightExpressionComboBoxTemplateSelector}"
                             >
    </telerik:RadComboBox>
</DataTemplate>