我在非静态类ColorManager(singleton)中有一个名为HeaderColorPairs的静态ObservableCollection>。
此属性绑定到ListView的ItemsSource,如下所示:
<ListView.ItemsSource>
<MultiBinding Converter="{StaticResource xmlFormatter}" ConverterParameter="ColorList">
<Binding Source="{StaticResource ResourceKey=colorManager}" Path="HeaderColorPairs"></Binding>
<Binding Path="LogEntryListViewModel.SelectedLogEntry.Model.RawXml"></Binding>
</MultiBinding>
</ListView.ItemsSource>
这就是转换方法:
public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
{
//parameter = outputtype
parameter = (parameter == null) ? "" : parameter;
string rawXml = value[1].ToString();
if (rawXml != DependencyProperty.UnsetValue.ToString() && rawXml != string.Empty)
{
OutputTypes opt = (OutputTypes)Enum.Parse(typeof(OutputTypes), parameter.ToString().Length.ToString());
if (opt == OutputTypes.Xml)
{
RawXmlFormatter rxf = new RawXmlFormatter(rawXml);
return rxf.XmlStringArrayToRunList();
}
else if (opt == OutputTypes.ColorList)
{
return ColorManager.GetColorsRelatedToXml(rawXml);
}
}
return null;
}
public static ObservableCollection<KeyValuePair<string, Color>> GetColorsRelatedToXml(string rawXml)
{
List<string> headerList = RawXmlFormatter.GetHeadersFromRawXml(rawXml);
SetUnsetColors(headerList);
return new ObservableCollection<KeyValuePair<string, Color>>(HeaderColorPairs.Where(kvp => headerList.Contains(kvp.Key)));
}
绑定工作得很好,它显示ObservableCollection的权限,但如果我在代码后面更改集合,ListView不会更新。
我现在使用了一个非常有效的非静态属性,而不是singleton模式(它也起作用)。问题已解决;)