【问题标题】:C# WPF value converter receiving entire collection instead of single item as value parameter?C# WPF 值转换器接收整个集合而不是单个项目作为值参数?
【发布时间】:2015-10-16 01:41:17
【问题描述】:

我有一个 C# WPF 4.51 应用程序。在我的一个 XAML 表单上,我有一个列表框,它的 ItemsSource 属性绑定到我的主 ViewModel 中属于 Collection 类型的属性.当 Collection 是 string 类型时,一切正常,我在列表框中看到了字符串集合。

但后来我将 Collection 中的类型更改为名为 ObservableStringExt 的类。该类有两个字段:StrItem 包含我想在列表框中显示的字符串,IsSelected 是一个支持字段。然后我创建了一个值转换器来提取 StrItem 字段并返回它。

但是,当我查看传递给值转换器的 Convert() 方法的 targetType 时,我看到了 IEnumerable 类型。鉴于该参数中的 Count 属性与预期的列表项的数量相匹配,看起来 Convert() 方法是接收对整个 Collection 的引用 ObservableStringExt,Collection中每一项的类型。这当然是个问题。这是什么原因造成的?我在 Windows Phone 和 WinRT(windows store 应用程序)中做过很多次这种事情,没有遇到任何麻烦。

这是值转换器的代码:

public class ObservableStringExtToStrItem : IValueConverter
{
    // The targetType of the value received is of type IEnumerable, not ObservableStringExt.
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is ObservableStringExt)
            return (value as ObservableStringExt).StrItem;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

下面是列表框的 XAML 代码。注意 Commands_FrequentyUsed 是主视图模型中的 ObservableCollectionWithFile 类型的属性,它是整个表单的数据上下文

<ListBox x:Name="listFrequentlyUsedCommands"
             Width="278"
             Height="236"
             Margin="30,103,0,0"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             ItemsSource="{Binding Commands_FrequentyUsed.Collection,
                                   Converter={StaticResource ObservableStringExtToStrItem}}" />

下面是包含列表框绑定的 Collection 的类和 Collection 包含的类的代码:

public class ObservableStringExt
{
    public string StrItem { get; set;}
    public bool IsSelected{ get; set; }
}

public class ObservableCollectionWithFile : BaseNotifyPropertyChanged
{

    public const string CollectionPropertyName = "Collection";
    private ObservableCollection<ObservableStringExt> _observableCollection = new ObservableCollection<ObservableStringExt>();

    public ObservableCollection<ObservableStringExt> Collection
    {
        get { return _observableCollection; }
        private set { SetField(ref _observableCollection, value); }
    }
} // public class ObservableCollectionWithFile

【问题讨论】:

  • value的实际类型呢,你查了吗?
  • 您的代码没有加起来。是的 ObservableCollectionWithFile 中的 Collection 是 ObservableCollection,好吧....但是 Commands_FrequentyUsed.Collection 的返回值可能不同
  • 我把代码放到了没有Commands_FrequentlyUserd部分的测试项目中。我可以将转换器中的值作为 ObservableCollection 获得,看起来非常好。
  • @csmh99 应该是 ObservableStringExt 类型,集合中的单个项目,而不是整个集合。
  • 您的意思是Commands_FrequentyUsed.Collection 只是一个项目吗?这显然是一个项目的集合。这就是将在转换中传递的内容(作为原始绑定值)。

标签: c# wpf xaml data-binding ivalueconverter


【解决方案1】:

我也遇到了同样的问题。不知道它应该如何正常工作,但更改转换器以也转换项目列表有帮助(我发现这比为列表创建单独的转换器更容易)

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var list = value as IEnumerable<ObservableStringExt>;
    if (list != null)
    {
        return list.Select(x => Convert(x, typeof(string), null, culture));
    }
    if (value is ObservableStringExt)
        return (value as ObservableStringExt).StrItem;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2014-06-24
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多