【问题标题】:CollectionViewSource Items binding with value converter与值转换器绑定的 CollectionViewSource 项
【发布时间】:2015-07-15 21:48:28
【问题描述】:

在我的 WPF 应用程序中,有一个自定义值转换器,其中 Convert 方法接收一个 ReadOnlyObservableCollection 值参数。

在 XAML 中通过以下绑定调用此值转换器:

<TextBlock Grid.Column="2" Text="{Binding Items, Converter={StaticResource totalCutsConverter}}"/>

Items 来自 CollectionViewSource 的位置,其分组描述如下:

<CollectionViewSource x:Key="sheetsViewSource" Source="{Binding}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="MaterialDescription" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

问题是:如何让我的值转换器接收我的特定项目类型(模型类)的集合而不是通用对象类?

换句话说,我想在我的 Convert 方法中进行以下转换

ReadOnlyObservableCollection<myClass> col = value as  ReadOnlyObservableCollection<myClass>();

有什么想法吗?

【问题讨论】:

  • IValueConverter 没有通用变体,因此您只需检查传入对象是否具有预期类型并将其转换到您的 Convert 方法中。
  • 您已经可以在您的Convert 方法中执行该转换,是什么阻止了您?另一个不同的问题是您的 value 对象实际上不是 ReadOnlyObservableCollection... 但这与转换器无关,毕竟您已绑定到该属性。
  • 大家好。我的问题不是关于如何实现值转换器或转换对象。真的。我的问题是如何使绑定机制传递我的特定对象类型的集合而不是通用集合。只是。如果这是不可能的,我可以忍受。但如果有其他方法,我想知道。

标签: c# wpf


【解决方案1】:

解决方案在cmets中已经提到过,就是在你的转换器中简单地转换值。真的没有必要为此实现任何花哨的东西。

话虽如此,我挑战了自己,如果你真的愿意,那么这里有一个例子来说明如何做到这一点:

public interface IValueConverter<T> : IValueConverter
{
    object Convert(T value, Type targetType, object parameter, CultureInfo culture);
    object ConvertBack(T value, Type targetType, object parameter, CultureInfo culture);
}

public abstract class GenericConverter<T> : IValueConverter<T>
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        CheckType(value);

        return Convert((T)value, targetType, parameter, culture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        CheckType(value);

        return ConvertBack((T)value, targetType, parameter, culture);
    }

    private void CheckType(object value)
    {
        if (value == null)
        {
            //TODO: Do something about nulls.
        }

        Type type = typeof(T);

        if (value.GetType() != type)
            throw new InvalidCastException(string.Format("Converter value could not be cast to: ", type.Name));
    }

    public abstract object Convert(T value, Type targetType, object parameter, CultureInfo culture);
    public abstract object ConvertBack(T value, Type targetType, object parameter, CultureInfo culture);
}

本质上,它只是一个普通的转换器,但在顶部添加了类型检查和强制转换。这是一个如何使用它的示例:

public class ExampleConverter : GenericConverter<string>
{
    public override object Convert(string value, Type targetType, object parameter, CultureInfo culture)
    {
        //TODO: Convert implementation

        return value;
    }

    public override object ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
    {
        //TODO: ConvertBack implementation

        return value;
    }
}

说实话,在转换器中简单地转换为您想要的值类型会容易得多,但是如果您需要,这里就在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-27
    • 2017-07-27
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多