我在非静态类 ColorManager(单例)中有一个名为 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 不会更新。