[英]C# WPF value converter receiving entire collection instead of single item as value parameter?
I have a C# WPF 4.51 application. 我有一个C#WPF 4.51应用程序。 On one of my XAML forms I have a list box that has its ItemsSource property bound to a property in my main ViewModel that is of type Collection .
在我的一种XAML表单上,我有一个列表框,该列表框的ItemsSource属性绑定到主ViewModel中类型为Collection的属性。 When the Collection was of type string , everything worked fine and I saw the collection of strings in the list box.
当Collection的类型为string时 ,一切工作正常,我在列表框中看到了字符串的集合。
But then I changed the type in the Collection to a class named ObservableStringExt . 但是后来我将Collection中的类型更改为名为ObservableStringExt的类。 The class has two fields: StrItem that contains the string I want displayed in the list box, and IsSelected , a supporting field.
该类有两个字段: StrItem ,它包含我要在列表框中显示的字符串; IsSelected ,它是一个支持字段。 I then created a value converter to extract the StrItem field and return it.
然后,我创建了一个值转换器以提取StrItem字段并返回它。
However, when I look at the targetType passed to the Convert() method of the value converter I see a type of IEnumerable . 但是,当我查看传递给值转换器的Convert()方法的targetType时,我看到了IEnumerable的类型。 Given that the Count property in that parameter matches the number of list items expected, it looks like the Convert() method is receiving a reference to the entire Collection instead of ObservableStringExt , the type of each item in the Collection.
假定该参数中的Count属性与预期的列表项数匹配,则看起来Convert()方法正在接收对整个Collection的引用, 而不是 ObservableStringExt (该Collection中每个项目的类型) 的引用 。 This of course is a problem.
这当然是一个问题。 What is causing this?
是什么原因造成的? I have done this sort of thing many times in Windows Phone and WinRT (windows store apps) many times without trouble.
我已经在Windows Phone和WinRT(Windows存储应用程序)中多次完成这种操作,而没有遇到麻烦。
Here is the code for the value converter: 这是值转换器的代码:
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();
}
}
Below is the XAML code for the list box. 下面是列表框的XAML代码。 Note Commands_FrequentyUsed is a property of type ObservableCollectionWithFile found in the main view model, which is the data context for the entire form:
注意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}}" />
Here is the code for the class that contains the Collection that the list box binds to and the class the Collection contains: 以下是包含列表框绑定到的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
I just had the same problem. 我只是有同样的问题。 Not sure how it should normally work, but changing the converter to also convert list of items helped (I found this easier than creating a separate converter for List)
不确定如何正常工作,但是将转换器更改为也转换项目列表很有帮助(我发现这比为List创建单独的转换器更容易)
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.